Created
April 29, 2013 20:39
-
-
Save fkztw/faccf2be88ab924a6091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream>; | |
#include <cstdlib>; | |
using namespace std; | |
int f(int n) | |
{ | |
int sum = 0; | |
while (n) | |
{ | |
sum += n%10; | |
n = n/10; | |
} | |
return sum; | |
} | |
int g(int n) | |
{ | |
int r = f(n); | |
if (1 <= r && r <= 9) return r; | |
else return g(r); | |
} | |
int main(void) | |
{ | |
int n; | |
while (cin >> n) | |
{ | |
if (n == 0) break; | |
else cout << g(n) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment