123456789101112131415161718192021222324252627282930313233343536373839 |
- #include <QCoreApplication>
- bool trouble(int num1, int num2);
- int main(int argc, char *argv[])
- {
- trouble(444,44);
- QCoreApplication a(argc, argv);
- return a.exec();
- }
- /* Create a function that takes two integers and returns true if a number
- * repeats three times in a row at any place in num1
- * AND that same number repeats two times in a row in num2.
- */
- bool trouble(int num1, int num2) {
- // convert the integers to strings to access each number
- std::string num1string = std::to_string (num1);
- std::string num2string = std::to_string (num2);
- std::vector <std::string> sVal;
- for(int i = 0;i<9;i++){
- char c = i + '0';
- int counter = 0;
- // when the counter hits 3, it will store the value into sVal
- for(int j = 0;j<num1string.size();j++){
- if(num1string[j] == c){
- if(3 == ++counter){
- sVal.push_back(std::string(c,2));
- break;
- }
- }
- // reset the counter when not in a row
- else{counter = 0;}
- }
- }
- // all numbers that repeat three times in a row have been stored into sVal
- for(int i = 0;i<sVal.size();i++){
- if(std::string::npos!= num2string.find(sVal[i])) {return true;}
- }
- return false;
- }
|