zoukankan      html  css  js  c++  java
  • HDU 1013 Digital Roots(字符串)

    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 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
     
    Answer
    虽然很水,但是我好像水了很久...
    题意是把数字各位相加,得到的和继续相加,直到和只有一位。
     
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <vector>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    #define msd memset(dp,0,sizeof(dp))
    using namespace std;
    //#define LOCAL
    string s;
    int t=0;
    void solve()
    {
        for(string::iterator it=s.begin(); it!=s.end(); it++)
            t+=*it-'0';
        s.clear();
        while(!(t%10==0&&t/10==0))
        {
            s.push_back((t%10)+'0');
            t/=10;
        }
    }
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        while(cin>>s&&*(s.begin())!='0')
        {
            solve();
            while(s.size()!=1)
            solve();
            printf("%c
    ",s[0]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    用css画三角形(提示框三角形)
    SpringMVC(2)之表单(非表单)参数绑定
    mvn jetty:run--编码GBK的不可映射字符
    Git命令及常见问题
    eclipse整合ssh框架基础
    css基础一(权重与position)
    sublime Text3下sass环境配置(windows)
    sublime Text3 设置多个浏览器预览
    初识iOS NSTimer 循环引用不释放问题
    ARC MARC 文件支持
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212588.html
Copyright © 2011-2022 走看看