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
  • 相关阅读:
    unnitest简单场景应用
    接口基础之request
    docker常用命令
    管理之心理学
    管理团队挑战和提升
    如何留下核心成员
    管理之面试技巧
    复杂接口请求怎样写http请求
    gitlab使用(一)
    不使用AutoLayout快速兼容适配iPhone6/6 Plus
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212588.html
Copyright © 2011-2022 走看看