(HDU)1013 --Digital Roots(数字根)

描述
正整数的数字根是通过对整数的数字求和得到的。 如果结果值是一个数字,那么该数字是数字根。 如果结果值包含两个或多个数字,则将这些数字相加,并重复该过程。 只要需要获得一位数字,就可以继续。例如,考虑正整数24.2和4相加产生值6.由于6是单个数字,6是24的数字根。现在考虑正整数39.3和9相加。由于12不是单个数字,所以必须重复该过程。 1和2相加得到3,3是一个单一的数字,也就是39的数字根。

输入
输入文件将包含一个正整数列表,每行一个。 输入的结束将由整数值零指示。

输出
对于输入中的每个整数,在输出的单独行上输出其数字根。

样品输入
24
39
0

示例输出
6
3

问题

想一下如何把一个数值分拆,每次%10,取得末位相加,再/10去掉末位,重复即可。

用字符串存每行的数值比较方便,具体看代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int fenjie(int num)
{
    int sum = 0;
    while(num>=10)
    {
        sum += num%10;
        num /= 10;
    }
        sum += num;
        return sum;
}

    int main(){

        char sum[1010];
    while(gets(sum)&&sum[0]!=‘0‘)
    {
        int i,x=0;
        for(i=0;i<strlen(sum);i++)
        {
            x += sum[i] - ‘0‘;
        }

        while(x>=10)
        {
            x = fenjie(x);
        }
        printf("%d\n",x);
    }
    return 0;
}

时间: 2024-11-03 21:57:46

(HDU)1013 --Digital Roots(数字根)的相关文章

zju 1115 Digital roots 数字根

#include <iostream> #include <string> using namespace std; int main() { string n; while(cin>>n,n!="0") { int s=0,l=n.length(); for(int i=0;i<l;i++) s+=n[i]-'0'; while(s>9) s=s/10+s%10; cout<<s<<endl; } return

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(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(两种方法,求数字根)

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【字符串,水】

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【模拟或数论】【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): 80782    Accepted Submission(s): 25278 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): 79180    Accepted Submission(s): 24760 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): 53090    Accepted Submission(s): 16577 Problem Description The digital root of a positive integer is found by summing the digits of