JosepRivaille

P55043: Rius digitals

May 28th, 2015
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int suma_digits(int n) {
  6.     int suma_digits = 0;
  7.     while (n != 0) {
  8.         suma_digits += n%10;
  9.         n /= 10;
  10.     }
  11.     return suma_digits;
  12. }
  13.  
  14.  
  15. int trobada_de_rius(int n) {
  16.     int riu_1 = 1;
  17.     int riu_3 = 3;
  18.     int riu_9 = 9;
  19.     while (riu_1 != n && riu_3 != n && riu_9 != n) {
  20.         if (riu_1 < n) riu_1 += suma_digits(riu_1);
  21.         else if (riu_3 < n) riu_3 += suma_digits(riu_3);
  22.         else if (riu_9 < n) riu_9 += suma_digits(riu_9);
  23.         else n += suma_digits(n);
  24.     }
  25.     return n;
  26. }
  27.  
  28. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment