HDU 1013

题目:HDU1013 Digital Roots

题目分析:刚开始没注意到输入数字特别大的情况,使用了输入整型数字,并且用了递归,然后就时间超时了。后来看别人的代码才知道这题是个“数论”题,仔细发现便可得result = (n-1)%9+1,而且输入用字符输入。

#include<stdio.h>
#include<string.h>
#include<math.h>

int main()
{
    int i,n,tmp;
    char a[1003];
    while (scanf("%s",&a)&&a[0]!='0')
    {
        n=0;
        for (i=0;i<strlen(a); i++)
        {
            n+=a[i]-'0';
        }
        printf("%d
",(n-1)%9+1);
    }
    return 0;
}
技进乎艺,艺进乎道
原文地址:https://www.cnblogs.com/weekend/p/5496945.html