2018-7-28第二次训练赛-E

E - Digital Roots

HDU - 1013

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

循环加法题目,唯一的小坑就是原始数据需要用字符串存储。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

char n[20000];

int math(char *a) {
    int sum = 0,sum2;
    while (*a) {
        sum += *a - '0';
        a++;
    }
    while (sum >= 10) {
        sum2 = 0;
        while (sum) {
            sum2 += sum%10;
            sum /= 10;
        }
        sum = sum2;
    }
    return sum;
}

int main()
{
    while (scanf("%s", n) && (*n!='0'||*(n+1)!='\0')) {
        printf("%d\n", math(n));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
功能强大的通用脱壳机.rar ╭═══════════════╮ ║ 零度软件园 ║ ╭══════┤ 网址 :www.05sun.cn ├══════╮ ║ ║ 电子书 :books.05sun.Com ║ ║ ║ ╰═══════════════╯ ║  ║ 大量的绿色软件下载,电影下载,音乐下载, ║ ║ 你最好的资源下载网站,每日大量更新海量资源。 ║ ║ 提供破解软件及商业软件注册,欢迎访问! ║  ║声明: ║ ║ 1) 本站不保证所提供软件或程序的完整性和安全性。 ║ ║ 2) 请在使用前查毒。 ║ ║ 3) 转载本站提供的资源请勿删除本说明文件。 ║ ║ 4) 本站提供的程序均为网上搜集,如果该程序涉及或侵害到您║ ║ 的版权请立即写信通知我们。 ║ ║ 5) 本站提供代码只可供研究使用,请在下载24小时内删除, ║ ║ 切勿用于商业用途,由此引起一切后果与本站无关。 ║ ║ ║ ║ 1. 推荐使用:WinRAR V3.4以上版本解压本站软件 ║ ║ 2. 本站空间支持:Www.05sun.com ║  ║ ║ ║ 优秀网站推荐: ║ ║ www.05sun.com books.05sun.com ║ ║ ║ ║ Oicq:2963458(广告合作) 3169873(软件收录) ║ ║ E-mail:mxder@163.Com ║ ║ 技术支持:Www.05sun.Com ║ ║ 本站致力于免费代码下载,国内原创的商业代码推广 ║ ║ 及商业注册,如果您有好的原创程序请联系我们! ║ ║ ╭───────────────────────╮ ║ ╰══┤ 零度软件园 http://www.05sun.com ├══╯ ╰─────────────
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值