题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013
题意:给一个大数,必须用数组存储,然后求它每个位数之和,如果小于10则输出结果,如果大于10,继续求它和的每位数之和。
例如:1000个9,和为9000,9000>10,再求9000每位数上的和,结果为9,符合题意,则输出。
<span style="font-size:18px;">#include <iostream>
#include <cstring>
using namespace std;
char num[10000];
int res(int n)
{
if(n < 10)
return n;
int i = 10;
int total = 0;
while(n/i)
{
total += (n % i / (i / 10));
i = i * 10;
}
total += n/(i/10);
return res(total);
}
void f(char *n)
{
int len = strlen(num);
for(int i = 0;i < len;i++)
a[i] = num[i] - 48;
int total = 0;
for(int i = 0;i < len;i++)
{
total += a[i];
}
cout << res(total) << endl;
}
int main(int argc, char *argv[])
{
while(1)
{
cin >> num;
if(num[0] == 48)
return 0;
f(num);
}
return 0;
}</span>