Guest User

Untitled

a guest
May 5th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. //Determine the number of positive integers x, where x is
  2. //less than equal to 9,999,999 and the sum of the digits in x is 31.
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.  
  10. int a=0;
  11. int b=0;
  12. int c=0;
  13. int d=0;
  14. int e=0;
  15. int f=0;
  16. int g=0;
  17. int solution=0;
  18.  
  19. while (1000000*a+100000*b+10000*c+1000*d+100*e+10*f+g<=9999999){
  20. if (a+b+c+d+e+f+g==31){
  21.         solution++;
  22.     if (g<9)
  23.        g++;
  24.     else{
  25.     if (f<9)
  26.             f++;
  27.     else{
  28.             f=0;
  29.             if (e<9)
  30.                 e++;
  31.             else{
  32.                 e=0;
  33.                 if (d<9)
  34.                     d++;
  35.                 else{
  36.                     d=0;
  37.                     if (c<9)
  38.                         c++;
  39.                     else{
  40.                         c=0;
  41.                         if (b<9)
  42.                             b++;
  43.                         else{
  44.                             b=0;
  45.                             if (a<9){
  46.                                 a++;}}}}}}}}}
  47. cout << solution;
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment