HDU1013 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

题目的意思大概就是将各个数位的数字加起来,假如比10小,就输出这个数,假如比10大就继续这个操作。

典型的一道数学题。

假如你知道一个神奇的东西叫做数根的话,就可以随意秒杀了,不过直接模拟也可以过。

一个数的数根:b = ( a - 1) % 9 + 1(a为所有数位的数的累加和)

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<string>
 6 using namespace std;
 7 int main(){
 8     char s1[1010];
 9     while(scanf("%s",&s1)&&s1[0]!=‘0‘)
10     {
11         int len=strlen(s1);
12         int r=0;
13         for (int i=0;i<len;++i)
14           r+=s1[i]-‘0‘;
15           printf("%d\n",(r-1)%9+1);
16     }
17     return 0;
18 }
时间: 2024-12-24 16:06:37

HDU1013 Digital Roots 模拟//数学题的相关文章

HDU1013 Digital Roots(解法二)

问题链接:HDU1013 Digital Roots.入门练习题,用C语言编写程序. 数根是指整数的各个位的数字之和.如果其和为1位整数,则为结果:如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止. 这个问题的大陷阱是,没有指出整数是多少位的.即使使用unsignde long long类型,也可能会溢出的.所以,需要先用字符串来处理. 之前的版本(参见:HDU1013 Digital Roots)不是最佳解,所以重新写了这个程序.一边输入一边处理,才是最佳的选择,可以省去存储空间.

解题报告: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() {

hdu1013 Digital Roots

思路: 九余数定理. http://blog.csdn.net/asd7f7/article/details/53994666 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 7 char a[1005]; 8 9 int main() 10 { 11 int n; 12 while (t

HDU1163 Eddy&#39;s digital Roots

问题链接:HDU1013 Digital Roots.入门练习题,用C语言编写程序. 这个问题是对于输入的n,计算n^n的数根. 先看一下以下式子: 因为:(10*a+b)*(10*a+b)=100*a*a+10*2*a*b+b*b 所以右边式子的数根(中间结果,也是左边式子的数根)为:a*a+2*a*b+b*b=(a+b)*(a+b) 故:对于两位数n,n*n的数根=n的树根×n的树根. 同理可以推出,对于任意位数的n,也满足:n*n的数根=n的树根×n的树根. 程序中,实现一个计算整数数根的

HDU1013 POJ1519 Digital Roots(解法三)

该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.入门练习题,用C语言编写程序. 问题简述:输入若干正整数,求其数根,直到输入为0为止. 问题分析:数根是指整数的各个位的数字之和.如果其和为1位整数,则为结果:如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止.这个问题的大陷阱是,没有指出整数是多少位的.即使使用unsignde long long类型,也可能会

HDU 1013.Digital Roots【模拟或数论】【8月16】

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,

HDU 1013 Digital Roots(两种方法,求数字根)

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

题目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

HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5783    Accepted Submission(s): 3180 Problem Description The digital root of a positive integer is found by summing the digit