hdoj 1013Digital 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*/
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
int main()
{
int sum;
char a[1000],n[1000];
while(gets(n))
{
if (strcmp(n,"0")==0)
{
break;
}
else
{
int sum=0;
for (int j=0;j<strlen(n);j++)
{
sum+=(n[j]-48);
}
while (sum>=10)
{
sprintf(a,"%d",sum);//把格式化的数据写入某个字符串缓冲区。 意思是把sum转化为字符串a。 sum=0;
for (int i=0;i<strlen(a);i++)
{
sum+=(a[i]-48);
}
}
printf("%d\n",sum);
}
}
}</span>
这种方法不easy想到。
<span style="font-size:18px;">//9余数定理
#include<stdio.h>
#include<string.h>
char a[10010];
int main()
{
int n;
while(~scanf("%s",a),strcmp(a,"0"))
{
int sum=0;
int len=strlen(a);
for(int i=0;i<len;++i)
sum+=a[i]-'0';
printf("%d\n",(sum-1)%9+1);//为什么减一后再加一。是为了避免18这些数字。
}
return 0;
}
</span>
hdoj 1013Digital Roots的更多相关文章
- HDU——1013Digital Roots(九余数定理)
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1163 Eddy's digital Roots(九余数定理的应用)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1013题Digital Roots 大数,9余数定理
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论
我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
随机推荐
- TeamTalk Android代码分析(业务流程篇)
TeamTalk Android代码分析(业务流程篇) 1.1 总体结构 1.总体结构有点类似MVC的感觉,模块结构从上向下大体是: UI层:Activity和Fragment构成,期间包括常用的一些 ...
- XTU1201:模和除
题目描写叙述 两个整数x和y,满足1<=x<=a,1<=y<=b 且x%y等于x/y的x和y的对数有多少? x%y是x除以y的余数, x/y是x除以y的商,即整数除. 输入 不 ...
- CSDN高校俱乐部第二届战神杯第二题题解
两个人玩一个数字游戏,给定两个正整数A,B,两个人轮流从一个数中减去另外一个数的正数倍.要保证结果非负, 首先得到0的人获胜. 比如:30 8经过一步操作能够变为22 8 或者14 8 或者 6 8. ...
- 又一次发现Oracle太美之glogin.sql
又一次发现Oracle太美之glogin.sql 刚開始接触Oracle的时候,有时候一登陆一个生产环境.常常会出现以下的情况: [oracle@rh64 app]$ sqlplus / as sys ...
- 2015.05.05,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 42
HOW TO TALK ABOUT WHAT GOES ON TEASER PREVIEW 一些以-ate结束的动词,通常表示: to exhaust([ig'zɔ:st] n. 排气,排气装置 v. ...
- 2015.04.20,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 30
1.brothers and sisters, wives and husbands Frater: brothers; soror: sister; uxor: wife; maritus: hus ...
- angularjs1-8,cacheFactory,sce
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- 【POJ 1082】 Calendar Game
[题目链接] http://poj.org/problem?id=1082 [算法] 对于每种状态,要么必胜,要么必败 记忆化搜索即可 [代码] #include <algorithm> ...
- SQL学习——基础语句(1)
简序 1.SQL,指结构化查询语言,全称是 Structured Query Language. 2.SQL 让您可以访问和处理数据库. 3.SQL 是一种 ANSI(American Nationa ...
- TF基础3
批标准化 批标准化(batch normalization,BN)是为了克服神经网络层数加深导致难以训练而诞生的.深度神经网络随着深度加深,收敛会越来越慢,会导致梯度弥散问题(vanishing gr ...