GDUFE ACM-1005

Digital Roots

Time Limit: 2000/1000ms (Java/Others)

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(the length of each integer will not exceed 1000), 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思路挺简单的附上AC代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int jys(int num)
 4 {
 5     int sum=0;
 6     while(num>=10)
 7     {
 8         sum+=num%10;
 9         num/=10;
10     }
11         sum+=num;
12         return sum;
13 }
14 int main()
15 {
16     char s[1010];
17     while(gets(s)&&s[0]!=‘0‘)
18     {
19         int i,x=0;
20         for(i=0;i<strlen(s);i++)
21         {
22             x+=s[i]-‘0‘;
23         }
24         while(x>=10)
25         {
26             x=jys(x);
27         }
28         printf("%d\n",x);
29     }
30     return 0;
31 }

在网上发现了一种更简单的代码(九余数定理):

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int s;
 5     char c;
 6     while(1)
 7     {
 8         s=0;
 9         while(scanf("%c",&c)&&c!=‘\n‘)
10             s+=c-‘0‘;
11         if(!s)return 0;
12         else if(s%9==0)puts("9");
13         else printf("%d\n",s%9);
14     }
15     return 0;
16 }
时间: 2024-10-08 11:13:20

GDUFE ACM-1005的相关文章

HDU ACM 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119732    Accepted Submission(s): 29072 Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A

杭电ACM 1005 Number Sequence

</p> Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38553    Accepted Submission(s): 8150 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n)

hdu acm 1005

#include <stdio.h> #include <iostream> using namespace std; int f(int n, int A, int B) { if(n==1) return 1; else if(n==2) return 1; else return (A * f(n - 1, A, B) + B * f(n - 2, A, B)) % 7; } int main() { for(int i=1; i<50; i++) { printf(&

poj 5001 Walk &amp;&amp;2014 ACM/ICPC Asia Regional Anshan Online 1005(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=5001 思路:dp计算出途径每个点的总概率,1-x即为所求解. dp题,先介绍下dp[i][j]为第j步走在第i个点的概率,那么dp[i][j]=dp[x1][j-1]+dp[x2][j-1]+...,x1,x2为i 的相邻节点.上一步在相邻节点这一步才能走到该点嘛. 每个点概率要一个一个的算,当算到第ii个点时(没打错,ii个点与代码对应),从起点推起,起点嘛,算是第0步吧,每个点被选中几率1.0/n,直接计

2016 ACM/ICPC Asia Regional Qingdao Online 1005 Balanced Game

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each pl

武汉科技大学ACM :1005: C语言程序设计教程(第三版)课后习题6.6

Problem Description 打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身. 例如:153是一个水仙花数,因为153=1^3+5^3+3^3. Output: 153 ??? ??? ??? Input 无 Output 所有的水仙花数,从小的开始. 每行一个 Sample Input Sample Output 1 #include<stdio.h> 2 3 #include<math.h>

华东交通大学2015年ACM“双基”程序设计竞赛1005

Problem E Time Limit : 3000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 357   Accepted Submission(s) : 16 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description 给定一个长度为n的数组 a[1],a[2],a[3]

武汉科技大学ACM :1005: 零起点学算法101——手机短号

Problem Description 大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号.假设所有的短号都是是 6+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678. 现在,如果给你一个11位长的手机号码,你能找出对应的短号吗? Input 输入数据的第一行是一个N(N <= 200),表示有N个数据,接下来的N行每一行为一个11位的手机号码. Output 输出应包括N行,每行包括一个对应的短号

http://acm.gdufe.edu.cn/Problem/read/id/1007

递推题目系列之一涂色问题 Time Limit: 2000/1000ms (Java/Others) Problem Description 有排成一行的n个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. Input: 输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0<n<=50). Output: 对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行.

ACM HDU 1005 Number Sequence

#include <cstdio> int f(int a,int b,int n) { int i,nn,n1,n2; if(n==1||n==2)return 1; a%=7; b%=7; n%=49; n1=n2=1; for(i=2; i<n; i++) { nn=(a*n2+b*n1)%7; n1=n2; n2=nn; } return nn; } int main(void) { int a,b,n; while(scanf("%d%d%d",&a