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
     
  • 相关阅读:
    输入输出那些事
    NYOJ 20
    NYOJ 283
    HDU 1285
    HDU 2639(第K大背包)
    HDU 4288
    对Visual Studio C++ hash_map严谨一点的测试转载
    vc6,vc.net,vc7,vc8,vc9,c,c++,c#的区别与联系
    我在南大的七年刘末鹏
    慎用Visual Studio C++默认的hash_map转载
  • 原文地址:https://www.cnblogs.com/sineatos/p/3269165.html
Copyright © 2011-2022 走看看