Loading...
More C++ Posts
#include <iostream>int main(){const char* const hello = "Hello, world!";const char* bruh = hello;char* const yeet = hello;std::cout << bruh << std::endl;std::cout << yeet << std::endl;return 0;}/*Place your bets!Will the program:a.) Print "Hello, world!" twice?b.) Compile error on line 5 (bruh initialize line) because the pointer gets implicit cast to non-const?c.) Compile error on line 7 (yeet initialize line) because the char gets implicit cast to non-const?d.) Both b and c?e.) Compile error line 11 (print yeet) because the pointer is constant and can't be incrementedf.) Print "Hello, world!" then print the pointer address in hexadecimalg.) Both b and e?h.) Both c and e?i.) B, c, and e?*/// The answer is in this base 64 string:// T25seSBjLikKVGhlIGNvbXBpbGVyIGRvZXNuJ3QgYXBwcmVjaWF0ZSB5b3UgbWFraW5nIHRoZSBjaGFyYWN0ZXJzIHRoZSBwb2ludGVyIHJlZmVycyB0byBub24tY29uc3QsIGJ1dCBpdCdzIGZpbmUgd2l0aCB5b3UgY29weWluZyBhIGNvbnN0YW50IHZhbHVlLCBpLmUuIHRoZSBwb2ludGVyLCB0byBhIG5vbi1jb25zdGFudCB2YXJpYWJsZS4KSWYgeW91IHJlcGxhY2UgdGhhdCBsaW5lIHdpdGggY2hhciogY29uc3QgeWVldCA9IGNvbnN0X2Nhc3Q8Y2hhciogY29uc3Q+KGhlbGxvKTsgSXQnbGwgcHJpbnQgIkhlbGxvLCB3b3JsZCEiIHR3aWNlLCB3aGljaCBpcyB2ZXJ5IHN0cmFuZ2UgY29uc2lkZXJpbmcgdGhhdCB5ZWV0IGlzIGEgY29uc3QgcG9pbnRlciwgc28geW91J2QgdGhpbmsgaXQgd291bGQgcHJpbnQgYXMgYSBoZXhhZGVjaW1hbCBiZWNhdXNlIGlmIHlvdSB0cnkgdG8gKCsreWVldCkgd2hpbGUgbG9vcGluZyB0aHJvdWdoIHRoZSBzdHJpbmcsIHlvdSdkIGdldCBhbiBlcnJvciwgYmVjYXVzZSBpdCdzIGNvbnN0IGFuZCBjYW4ndCBiZSBjaGFuZ2VkLgpJbnN0ZWFkIG9mIHVzaW5nIGEgdGVtcGxhdGUgZnVuY3Rpb24gZm9yIG9zdHJlYW06Om9wZXJhdG9yPDwsIHRoZXkgbWFrZSBpdCBhIGZ1bmN0aW9uIHRoYXQgdGFrZXMgdHlwZSBjb25zdCBjaGFyKiwgYW5kIEMrKyBoYXMgbm8gcHJvYmxlbXMgcHJvbW90aW5nIGEgdmFyaWFibGUgdG8gY29uc3RhbnQgd2hlbiBpbXBsaWNpdCBjYXN0aW5nLCBhbmQgaXQgaGFzIG5vIHByb2JsZW1zIGltcGxpY2l0IGNhc3RpbmcgdGhlIGNvbnN0IHBvaW50ZXIgdG8gYSBub3JtYWwgcG9pbnRlciBiZWNhdXNlIGl0J3MgbWFraW5nIGEgY29weS4gVGhlIHBvaW50ZXIgZ2V0cyBjb3BpZWQgYmVjYXVzZSB0aGUgcG9pbnRlciBpcyBwYXNzZWQgYnkgdmFsdWUsIG5vdCByZWZlcmVuY2Uu
// Iterative C++ program to// implement Stein's Algorithm//#include <bits/stdc++.h>#include <bitset>using namespace std;// Function to implement// Stein's Algorithmint gcd(int a, int b){/* GCD(0, b) == b; GCD(a, 0) == a,GCD(0, 0) == 0 */if (a == 0)return b;if (b == 0)return a;/*Finding K, where K is thegreatest power of 2that divides both a and b. */int k;for (k = 0; ((a | b) & 1) == 0; ++k){a >>= 1;b >>= 1;}/* Dividing a by 2 until a becomes odd */while ((a & 1) == 0)a >>= 1;/* From here on, 'a' is always odd. */do{/* If b is even, remove all factor of 2 in b */while ((b & 1) == 0)b >>= 1;/* Now a and b are both odd.Swap if necessary so a <= b,then set b = b - a (which is even).*/if (a > b)swap(a, b); // Swap u and v.b = (b - a);} while (b != 0);/* restore common factors of 2 */return a << k;}// Driver codeint main(){int a = 12, b = 780;printf("Gcd of given numbers is %d\n", gcd(a, b));return 0;}
#include <iostream>#include <vector>using namespace std;void swap(int *a, int *b){int temp = *b;*b = *a;*a = temp;}void heapify(vector<int> &hT, int i){int size = hT.size();int largest = i;int l = 2 * i + 1;int r = 2 * i + 2;if (l < size && hT[l] > hT[largest])largest = l;if (r < size && hT[r] > hT[largest])largest = r;if (largest != i){swap(&hT[i], &hT[largest]);heapify(hT, largest);}}void insert(vector<int> &hT, int newNum){int size = hT.size();if (size == 0){hT.push_back(newNum);}else{hT.push_back(newNum);for (int i = size / 2 - 1; i >= 0; i--){heapify(hT, i);}}}void deleteNode(vector<int> &hT, int num){int size = hT.size();int i;for (i = 0; i < size; i++){if (num == hT[i])break;}swap(&hT[i], &hT[size - 1]);hT.pop_back();for (int i = size / 2 - 1; i >= 0; i--){heapify(hT, i);}}void printArray(vector<int> &hT){for (int i = 0; i < hT.size(); ++i)cout << hT[i] << " ";cout << "\n";}int main(){vector<int> heapTree;insert(heapTree, 3);insert(heapTree, 4);insert(heapTree, 9);insert(heapTree, 5);insert(heapTree, 2);cout << "Max-Heap array: ";printArray(heapTree);deleteNode(heapTree, 4);cout << "After deleting an element: ";printArray(heapTree);}
#include <iostream>using namespace std;int main {cout << 1;}
#include <iostream>#include <vector>#include <limits>#define DEBUG_TRIAL falseclass 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_TRIALstd::cout << "Start: " << start << ". End: " << end << ". Floors Left: " << floorsLeft() << ". Middle Index: " << middle() << std::endl;#endifdrops++;bool cracked = height >= BREAKING_HEIGHT;if(cracked) --eggs;//Update the recordrecord[height] = (height >= BREAKING_HEIGHT)? 'X' : 'O';#if DEBUG_TRIAL//Print the recordstd::cout << record << std::endl;#endifreturn cracked;}size_t nowWhat(){if(foundAnswer()){return drops;}else if(eggs <= 0){ //Ran out of eggsthrow "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 breaksif(drop(middle())){end -= (floorsLeft()/2UL);}else{ //egg doesn't crackstart += (floorsLeft()/2UL);}return nowWhat();}//returns the amount of drops needed to find the answersize_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 answersize_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 resultsstd::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 dropsstats.mean = static_cast<double>(totaldrops) / HEIGHT;return stats;}//Benchmarks a single building height from 1 egg to MAX_EGGSvoid 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_HEIGHTvoid benchmark(const size_t MAX_HEIGHT){const size_t MAX_EGGS = 2;//Test every buildingfor (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;}
#include <iostream>#include <cstring>int main(int argc, char** argv){//With decimalif(strstr(argv[1], ".") != nullptr){int i = 0;//Skip i to first non 0 digitwhile(argv[1][i] < '1' || argv[1][i] > '9') ++i;//If digit comes before decimalif((argv[1] + i) < strstr(argv[1], ".")){ //Good example of pointer arithmeticstd::cout << strlen(argv[1] + i) - 1 << std::endl; //Another good example}else{//If digit is after decimalstd::cout << strlen(argv[1] + i) << std::endl;}}else{//Without decimalint m = 0;int i = 0;while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //In case of some number like 0045for(; argv[1][i] != '\0'; ++i){if(argv[1][i] >= '1' && argv[1][i] <= '9') m = i + 1;}std::cout << m << std::endl;}return 0;}