main.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <QCoreApplication>
  2. bool trouble(int num1, int num2);
  3. int main(int argc, char *argv[])
  4. {
  5. trouble(444,44);
  6. QCoreApplication a(argc, argv);
  7. return a.exec();
  8. }
  9. /* Create a function that takes two integers and returns true if a number
  10. * repeats three times in a row at any place in num1
  11. * AND that same number repeats two times in a row in num2.
  12. */
  13. bool trouble(int num1, int num2) {
  14. // convert the integers to strings to access each number
  15. std::string num1string = std::to_string (num1);
  16. std::string num2string = std::to_string (num2);
  17. std::vector <std::string> sVal;
  18. for(int i = 0;i<9;i++){
  19. char c = i + '0';
  20. int counter = 0;
  21. // when the counter hits 3, it will store the value into sVal
  22. for(int j = 0;j<num1string.size();j++){
  23. if(num1string[j] == c){
  24. if(3 == ++counter){
  25. sVal.push_back(std::string(c,2));
  26. break;
  27. }
  28. }
  29. // reset the counter when not in a row
  30. else{counter = 0;}
  31. }
  32. }
  33. // all numbers that repeat three times in a row have been stored into sVal
  34. for(int i = 0;i<sVal.size();i++){
  35. if(std::string::npos!= num2string.find(sVal[i])) {return true;}
  36. }
  37. return false;
  38. }