zoukankan      html  css  js  c++  java
  • 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
     
  • 相关阅读:
    Luogu3119 [USACO15JAN]Grass Cownoisseur G
    BZOJ4361 isn
    洛谷1330 封锁阳光大学
    codechef AUG17 T2 Chef and Mover
    codechef AUG17 T1 Chef and Rainbow Array
    【bzoj3211】花神游历各国&&【bzoj3038】上帝造题的七分钟2
    noip 瑞士轮 ————归并排序解法
    记录string的妙用
    洛谷P1550 [USACO08OCT]打井Watering Hole
    汕头市队赛 SRM 09 C 撕书
  • 原文地址:https://www.cnblogs.com/sineatos/p/3269165.html
Copyright © 2011-2022 走看看