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
     
  • 相关阅读:
    编程心得2----有疑惑,用代码说话
    Java的static关键字
    html中设置textbox的宽和高
    C#(winform)设置窗体的启动位置
    C#(winform)设置窗口置顶
    C#(winform)实现不同DPI控件自适应1
    设计模式--策略模式(strategy)
    requirejs原理深究以及r.js和gulp的打包【转】
    JavaScript中清空数组的三种方式
    localstorage本地存储
  • 原文地址:https://www.cnblogs.com/sineatos/p/3269165.html
Copyright © 2011-2022 走看看