题目1124:Digital Roots (方法超简单)

题目1124:Digital Roots

学到的新知识

求一个数各个的和可以把其%9就行,例如13%9=4 11%9=2;123%9=6;

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3819

解决:1335

题目描述:

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.

输入:

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.

输出:

For each integer in the input, output its digital root on a separate line of the output.

样例输入:
24
39
0
样例输出:
6
3
提示:

The integer may consist of a large number of digits.

//题目中说数很大,可能真的很大,需要用字符数组来存储
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

int main()
{
    int i,m;
    char s[100];
    while(scanf("%s",s)!=EOF&&s[0]!=‘0‘)
    {
        for(m=i=0;s[i];i++)
            m+=s[i]-‘0‘;
        printf("%d\n",m%9==0?9:m%9);//这个人是天才哈,求一个数各个的和可以把其%9就行,例如13%9=4 11%9=2;123%9=6;
    }
    return 0;
}

时间: 2024-12-30 12:47:13

题目1124:Digital Roots (方法超简单)的相关文章

ZOJ 1115 Digital Roots(简单,字符串与数)

题目 //好一道水水题,可是我居然也错了那么多次,后来百度来发现是因为数据数位可能很长很长,要用字符串数组... //简单 //有坑啊——数据可能很大很大,要用字符串表示! #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char s[1010]; while(scanf("%s",s)!=EOF) { if(strc

HDU OJ Digital Roots 题目1013

 /*Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 58506    Accepted Submission(s): 18275 Problem Description The digital root of a positive integer is found by summing the digi

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

杭电ACM1163——Eddy&#39;s digital Roots

这题主要是找规律,第一次找出来!~~ 题目的意思是求一个数的digital root,这个所谓的digital root也就是一个数的各位数之和,如果这个数的两位数以上,重复再算digital root,直到这个数是一位数. 这一题就是求n^n的digital root. 规律如下: n个n相乘的结果假设为S,S的digital root 等于这n个数的digital root的相乘. 下面的是AC的代码,很简单的: #include <iostream> using namespace st

解题报告:hdu1013 Digital Roots

2017-09-07 22:02:01 writer:pprp 简单的水题,但是需要对最初的部分进行处理,防止溢出 /* @theme: hdu 1013 Digital roots @writer:pprp @begin:21:52 @end:21:59 @error:虽然是水题,但是还是需要对最初的处理,如果过大超过了int范围,就出错了 @date:2017/9/7 */ #include <bits/stdc++.h> using namespace std; int main() {

HDU 1013 Digital Roots【字符串,水】

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 79339    Accepted Submission(s): 24800 Problem Description The digital root of a positive integer is found by summing the digits of

ACM——Digital Roots

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:493            测试通过:175 描述 The digital root of a positive integer is found by summing the digits of

poj 3112 Digital Biochemist Circuit(简单题)

题目链接:http://poj.org/problem?id=3112 Digital Biochemist Circuit Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 876   Accepted: 375 Description A digital biochemist circuit (DBC) is a device composed of a set of processing nodes. Each pro

HDU 1013 Digital Roots

算法爱好者 HDU 1013 Digital Roots Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29353    Accepted Submission(s): 8974Problem Description The digital root of a positive integer is foun