Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468. Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420. We shall call a positive integer that is neither increasing nor decreasing a "bouncy" number; for example, 155349. Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy. In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538. Surprisingly, bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90%. Find the least number for which the proportion of bouncy numbers is exactly 99%.
#include <iostream> #include <vector> #include <algorithm> bool bouncyCheck(int passedNumb); int main(void) { double percentCheck = 0; unsigned long long bouncy = 0, unbouncy = 99; // all two digit are unbouncy, so set to 99 for(int i = 100; percentCheck != 99; i++) { if(bouncyCheck(i)) // if determiend to be bouncy { bouncy++; } else // it is unbouncy { unbouncy++; } percentCheck = ( (100.0 / (bouncy + unbouncy)) * bouncy); // calculate percentage } std::cout << "The answer is" << bouncy + unbouncy; } bool bouncyCheck(int passedNumb) { std::vector<int> digBuff, sortBuff; while(passedNumb) // separate into vector of digits { digBuff.insert(digBuff.begin(), passedNumb % 10); passedNumb /= 10; } sortBuff = digBuff; // copy std::sort(sortBuff.begin(), sortBuff.end()); // sort to check for ascending/descending if(sortBuff == digBuff) // if equal, we know it is not bouncy { return false; } std::reverse(sortBuff.begin(), sortBuff.end()); if(sortBuff == digBuff) // if equal, we know it is not bouncy { return false; } return true; }