今天來解YKL27(UVA11332):Summing Digits
跟題目敘述的一樣
f(n) = 1+2+3+4+5+6+7+8+9+2 = 47
f(f(n)) = 4 + 7 = 11
f(f(f(n))) = 1 + 1 = 2
用下方
sum += num%10;
num/=10;
#include <iostream>
#include <string>
using namespace std;
int plusdig(int num){
while(num >= 10){
int sum=0;
while(num > 0){
sum += num%10;
num/=10;
}
num = sum;
}
return num;
}
int main(){
int num;
while(cin >> num){
if(num==0)break;
cout << plusdig(num) << endl;
}
return 0;
}