题意:For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39
思路:刚开始我提交也错了,原来是高精度的,所以用数组做就得了。
代码如下:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[3000];
while(cin>>s&&strcmp(s,"0")!=0)
{
int n=0,i;
for(i=0;i<strlen(s);i++)
n+=s[i]-'0';
int sum=0;
while(n)
{
sum+=n%10;
n/=10;
if(sum>9&&n/10==0)
{
n=sum;
sum=0;
}
}
cout<<sum<<endl;
}
return 0;
}