您的位置:首页 > 其它

hdu 1013 Digital Roots (九余数定理)

2017-09-23 18:45 363 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013

题目大意:给定一个数,这个数的各位上的数相加的和,若这个数小于10,则相加的和为这个数的数根;反之,就重复操作,知道和小于10,得到这个数的数根。这个数可能很大,所以要用字符串来存储。

分析:根据题目大意简单模拟一边,肯定都会。现在利用另一种方法,9余数定理。定理证明:http://www.cnblogs.com/zzqc/p/6684794.html

ac代码

#include<cstdio>
#include<cstring>
#define M 100005

char a[M];

int main()
{
while(scanf("%s", a))
{
int num = 0;
int len = strlen(a);
for(int i = 0; i < len; i++)
num = num + a[i] - '0';
if(num == 0)return 0;
int root = num % 9;
if(root == 0)printf("9\n");
else printf("%d\n", root);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: