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",(n-1)%9+1);
    }
    return 0;
}
时间: 2024-11-20 12:42:47

HDU 1013的相关文章

HDU 1013 Digital Roots

算法爱好者 HDU 1013 Digital Roots Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29353    Accepted Submission(s): 8974Problem Description The digital root of a positive integer is foun

HDU 1013 Digital Roots 题解

Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits a

HDU 1013 Digital Roots【字符串,水】

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 79339    Accepted Submission(s): 24800 Problem Description The digital root of a positive integer is found by summing the digits of

HDU 1013 Digital Roots(两种方法,求数字根)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 67949    Accepted Submission(s): 21237 Problem Description The digital root of a positive integer is found by summing the digits of

HDU 1013 Digital Roots(to_string的具体运用)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 90108    Accepted Submission(s): 28027 Problem Description The digital root of a

hdu 1013 Digital Roots(数论 模拟)

#include<cstdio> #include<cstring> #include<iostream> using namespace std; char num[10000]; int get_sum() { int ret=0; for(int i=0;i<strlen(num);i++) { ret+=(num[i]-'0')%10; } return ret; } int main() { int i,j,k; while(scanf("%s

数学/hdu 1013 Digital Roots

题意 求一个数的数根,即各位数之和,再之和,直到为个位数 分析 首先,要知道这样一个结论: 任何一个整数模9同余于它的各数位上数字之和 具体证明过程如下: 设自然数N=a[n]a[n-1]…a[0],其中a[0],a[1].….a[n]分别是个位.十位.…上的数字 再设M=a[0]+a[1]+…+a[n] 求证:N≡M(mod 9). 证明:    ∵ N=a[n]a[n-1]…a[0]=a[n]*10^n+a[n-1]*10^(n-1)+…+a[1]*10+a[0].   又∵ 1≡1(mod

HDU 1013.Digital Roots【模拟或数论】【8月16】

Digital Roots Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits,

hdu 1013 Digital Roots 用一个大水题来纪念我进入杭电前一万名

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 53090    Accepted Submission(s): 16577 Problem Description The digital root of a positive integer is found by summing the digits of