• HDU


    先上题目:

    Digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 39890    Accepted Submission(s): 12286


    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 are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

    For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
     
    Input
    The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
     
    Output
    For each integer in the input, output its digital root on a separate line of the output.
     
    Sample Input
    24 39 0
     
    Sample Output
    6 3
     
      题意是给你一个数字,求出它的各位数字之和,如果结果不是小于10的,就继续前面的操作,输出最后结果。
      其实这一题没有什么好说,就一个地方有点坑,明明说好了是integer,输入竟然有可能超过int,要用字符串读入才可以过= =。
     
    上代码:
     
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    char s[10000];
    
    int main()
    {
        int n,a,len;
        //freopen("data.txt","r",stdin);
        while(1)
        {
            scanf("%s",s);
            n=0;
            len=strlen(s);
            while(len--){n+=s[len]-'0';}
            if(n==0) break;
            while(n>=10)
            {
                a=0;
                while(n){a+=n%10; n/=10;}
                n=a;
            }
    
            printf("%d
    ",n);
        }
        return 0;
    }
    1013
     
  • 相关阅读:
    谨以此文纪念一周的心血历程
    面向对象初调用:foolish 电梯
    洛谷 1016 旅行家的预算
    洛谷 1514 引水入城
    洛谷 3178 树上操作
    洛谷 3811 【模板】乘法逆元
    洛谷 1156 垃圾陷阱
    洛谷 1363 幻想迷宫
    洛谷 1736 创意吃鱼法
    洛谷 1436 棋盘分割
  • 原文地址:https://www.cnblogs.com/sineatos/p/3269165.html
Copyright © 2020-2023  润新知