刚开始的时候,代码里有注释,结果CE。
后来,WA,奇了怪了。挺简单的一道题哇。
事实上是我没考虑大整数,不能用int,得用字符处理。
方法二:http://blog.sina.com.cn/s/blog_6ce23f8f0100loc4.html,各个位数的和%9。
# include <stdio.h>
# include <string.h>
int main()
{
int root;
char a;
while(1)
{
root=0;
while(scanf("%c",&a) && a!='\n')
root+=a-'0'; //所有位数上的数字和
if(root==0)
break;
if(root%9==0)
root=9;
else
root=root%9;
printf("%d\n",root);
}
return 0;
}
方法一(C++):大学上了两年多,却只能看懂简单的。C++中的string,代码来源:http://blog.youkuaiyun.com/bbplayers/article/details/5714677
// may the input string will be long ,and then 'int' is not long enough
#include<iostream>
#include<string>
using namespace std;
int digitroot( int a)
{
int dr = 0;
while( a > 0)
{
dr += a%10;
a /= 10;
}
if( dr > 9)
return digitroot( dr);
return dr;
}
int main(void)
{
string s;
while( cin >> s && s[0] != '0' )
{
int a = 0;
for( int i=0; i< s.length(); i++)
a += s[i]-'0';
cout<<digitroot( a)<<endl;
}
return 0;
}