Skip to main content

Hello World!

Aug 31, 2020joshwrou
Loading...

More C++ Posts

Critques

Feb 4, 2021aedrarian

0 likes • 0 views

#include <iostream>
using namespace std;
main
{
cout << "No tabbing. That's very sad :(\n";
cout << "No in-editor highlighting either :(((\n";
cout << "Descriptions might be niice too.";
}

Test

Jun 17, 2024oceantran27

0 likes • 2 views

#include <iostream>
using namespace std;
int main {
cout << 1;
}

Literal Bruh

Jul 30, 2023LeifMessinger

1 like • 5 views

//Constant prefix notation solver using bruh
//Could make it infix or postfix later
#include<string>
#include<vector>
#include<iostream>
std::vector<long double> bruhBuff;
long double operator ""bruh(long double a){
bruhBuff.push_back(a);
return a;
}
long double operator ""bruh(const char op){
if(bruhBuff.size() < 2) throw "Bruh weak";
long double b = bruhBuff.back();
bruhBuff.pop_back();
long double a = bruhBuff.back();
bruhBuff.pop_back();
switch(op){
case (int)('+'):
return a + b;
case (int)('-'):
return a - b;
case (int)('*'):
return a * b;
case (int)('/'):
return a / b;
}
return 69l;
}
int main(){
1.0bruh;
2.0bruh;
std::cout << '+'bruh << std::endl;
return 0;
}

set hostname syscall

Oct 7, 2023AustinLeath

0 likes • 12 views

#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/utsname.h>
int main() {
char newHostname[] = "newhostname"; // Replace with the desired hostname
if (sethostname(newHostname, strlen(newHostname)) == 0) {
std::cout << "Hostname set to: " << newHostname << std::endl;
// Optionally, update the /etc/hostname file to make the change permanent
FILE *hostnameFile = fopen("/etc/hostname", "w");
if (hostnameFile != NULL) {
fprintf(hostnameFile, "%s\n", newHostname);
fclose(hostnameFile);
} else {
perror("Failed to update /etc/hostname");
}
} else {
perror("Failed to set hostname");
}
return 0;
}

Egg Problem Template

Jul 10, 2023LeifMessinger

0 likes • 4 views

#include <iostream>
#include <vector>
#include <limits>
#define DEBUG_TRIAL false
class Trial{
public:
const size_t HEIGHT;
std::string record;
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
//Eggs is the eggs remaining.
//Start is the bottom floor.
//End is one above the top floor.
const size_t BREAKING_HEIGHT;
size_t eggs;
size_t start;
size_t end;
size_t floorsLeft(){
return (end-start);
}
size_t middle(){
return start + (floorsLeft()/2UL);
}
size_t drops = 0;
Trial(const size_t BREAKING_HEIGHT, size_t eggs, size_t start, size_t end): BREAKING_HEIGHT(BREAKING_HEIGHT), eggs(eggs), start(start), end(end), HEIGHT(end), record(end, '_'){
record[BREAKING_HEIGHT] = 'B'; //Marking the breaking point
}
bool foundAnswer(){
return ((record[0] == 'X') || (record.find("OX")!=std::string::npos));
}
//returns true if the egg broke.
//height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
bool drop(size_t height){
#if DEBUG_TRIAL
std::cout << "Start: " << start << ". End: " << end << ". Floors Left: " << floorsLeft() << ". Middle Index: " << middle() << std::endl;
#endif
drops++;
bool cracked = height >= BREAKING_HEIGHT;
if(cracked) --eggs;
//Update the record
record[height] = (height >= BREAKING_HEIGHT)? 'X' : 'O';
#if DEBUG_TRIAL
//Print the record
std::cout << record << std::endl;
#endif
return cracked;
}
size_t nowWhat(){
if(foundAnswer()){
return drops;
}else if(eggs <= 0){ //Ran out of eggs
throw "Algorithm failed! No more eggs!";
return 1UL;
}else if(eggs > 1){
return wrecklessSearch();
}else{
return safeSearch();
}
}
size_t safeSearch(){
if(drop(start)){
--end;
}else{
++start;
}
return nowWhat();
}
size_t wrecklessSearch(){
//If the egg breaks
if(drop(middle())){
end -= (floorsLeft()/2UL);
}else{ //egg doesn't crack
start += (floorsLeft()/2UL);
}
return nowWhat();
}
//returns the amount of drops needed to find the answer
size_t search(){
return nowWhat();
}
};
//Height is the height of the building in floors.
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
//Eggs is the eggs given.
//returns the amount of drops needed to find the answer
size_t search(const size_t height, const size_t BREAKING_HEIGHT, size_t eggs){
Trial trial(BREAKING_HEIGHT, eggs, 0, height);
return trial.search();
}
class TrialStats {
public:
size_t min = std::numeric_limits<size_t>::max();
size_t max = 0;
double mean = -1.0;
void printStats(){
// Print the results
std::cout << "Minimum drops: " << min << std::endl;
std::cout << "Maximum drops: " << max << std::endl;
std::cout << "Mean drops: " << mean << std::endl;
}
};
//Benchmarks all the possible breaking points of a single building height with a number of eggs.
TrialStats trial(const size_t HEIGHT, const size_t eggs){
TrialStats stats;
int totaldrops = 0;
//Test every possible breaking point
//Breaking height is the index of the floor, so 0 is the bottom floor, height-1 is the top floor.
for (int breakingHeight = 0; breakingHeight < HEIGHT; ++breakingHeight) {
size_t drops = search(HEIGHT, breakingHeight, eggs);
stats.min = std::min(stats.min, drops);
stats.max = std::max(stats.max, drops);
totaldrops += drops;
}
// Calculate the mean number of drops
stats.mean = static_cast<double>(totaldrops) / HEIGHT;
return stats;
}
//Benchmarks a single building height from 1 egg to MAX_EGGS
void testTower(const size_t height, const size_t MAX_EGGS){
//Drop every amount of eggs that you'd need.
for (int eggs = 1; eggs <= MAX_EGGS; ++eggs) {
std::cout << "Building height: " << height << ". Num eggs: " << eggs << std::endl;
TrialStats stats = trial(height, eggs);
stats.printStats();
std::cout << std::endl << std::endl;
}
}
//Benchmarks all buildings from 0 to MAX_HEIGHT
void benchmark(const size_t MAX_HEIGHT){
const size_t MAX_EGGS = 2;
//Test every building
for (size_t height = 1; height <= MAX_HEIGHT; ++height) {
testTower(height, std::min(height, MAX_EGGS));
}
}
int main() {
constexpr size_t MAX_HEIGHT = 36;
benchmark(MAX_HEIGHT);
return 0;
}

Simple Greedy sort C++

Jun 30, 2023Iceman_71

0 likes • 7 views

#include <iostream>
using namespace std;
int main()
{
int arr[] = {5, 1, 4, 20, 10, 2, 13, 11, 6, 21};
int greed[] = {0, 0, 0, 0};
int k = 0;
int i;
int set_index;
while (k < 4)
{
i = 0;
while (i < 10)
{
if (arr[i] > greed[k])
{
greed[k] = arr[i];
set_index = i;
}
i++;
}
arr[set_index] = 0;
k++;
}
cout << greed[0] << " " << greed[1] << " " << greed[2] << " " << greed[3] << endl;
}