标签:algorithm
#include <cstdio> #include <iostream> #include <string> using namespace std; #ifdef ONLINE_JUDGE #define FINPUT(file) 0 #define FOUTPUT(file) 0 #else #define FINPUT(file) freopen(file,"r",stdin) #define FOUTPUT(file) freopen(file,"w",stdout) #endif unsigned int sumOfDigits(string s) { unsigned int sum = 0; int i = 0; while(i<s.length()) { sum += s[i]-‘0‘; i++; } return sum; } unsigned int sumOfDigits2(unsigned int n) { unsigned int sum = 0; while(n!=0) { sum += n%10; n/=10; } return sum; } int main() { FINPUT("in.txt"); FOUTPUT("out.txt"); string s; while(cin>>s && s!="0") { int sum = sumOfDigits(s); while(sum>=10) { sum = sumOfDigits2(sum); } cout<<sum<<endl; } return 0; }
标签:algorithm
原文地址:http://blog.csdn.net/daringpig/article/details/25461611