刚开始的时候,代码里有注释,结果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;
}
这是一篇关于ZOJ 1115题目的博客,作者在解决过程中遇到了错误,从CE到WA,最终发现问题是由于使用int处理大整数导致,解决方案转为使用字符处理。博客提到了两种方法,一种是通过字符处理各个位数的和对9取余,另一种是C++中使用string的方法。
204

被折叠的 条评论
为什么被折叠?



