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
  • 相关阅读:
    WDA学习(14):Colors Cell and Input Enable to ALV Column
    JavaScript try-catch语句(错误处理)
    constructor
    input file 重复上传同一张图片失效的解决办法
    input下拉带输入框
    box-shaw四边阴影详解
    苹方字体合集
    两个div不同高度并排显示
    弹窗库
    webstorm破解安装版本
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212588.html
Copyright © 2011-2022 走看看