hdu_1013_Digital Roots_201310121652
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40786 Accepted Submission(s): 12584
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.
- #include <stdio.h>
- int f(int t)
- {
- int s=;
- while(t>)
- {
- s+=t%;
- t/=;
- }
- return s;
- }
- int main()
- {
- int n;
- while(scanf("%d",&n),n)
- {
- int i,j,t,s;
- t=n;
- if(t<)
- printf("%d\n",t);
- else if(t>=)
- {
- s=f(t);
- while(s>=)
- {
- s=f(s);
- }
- printf("%d\n",s);
- }
- }
- return ;
- }
- //wa
第二次做的:
- #include <stdio.h>
- #include <string.h>
- char s[];
- int f(int t)
- {
- int s=;
- while(t>)
- {
- s+=t%;
- t/=;
- }
- return s;
- }
- int main()
- {
- while(scanf("%s",s)&&s[]!='')
- {
- int i,t,sum=;
- for(i=;i<strlen(s);i++)
- sum+=s[i]-'';
- if(sum<)
- printf("%d\n",sum);
- else if(sum>=)
- {
- t=f(sum);
- while(t>=)
- {
- t=f(t);
- }
- printf("%d\n",t);
- }
- }
- return ;
- }
- //ac
链接(大神做法):http://www.cppblog.com/ArcTan/articles/167330.html
hdu1013(模拟&数论)
这个题模拟也可以AC,刚开始我也是模拟AC的。不过看了百度看了大牛的博客,感谢大牛,知道了还有数论这回事。
n=0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......... 100 101 102 103 ....
roots=0 1 2 3 4 5 6 7 8 9 1 2 3 4 .......1 2 3 4....
原来是以1.....9为循环节的。想想也是,每次增加1,那么层层迭代下来,最终当ans<10的时候也是每次增加了1。如此,可以归纳出
roots=(n-1)%9+1
注意输入的数字很大需要字符串读入,求和得n:
#include<string.h>
#include<math.h>
int main()
{
int i,n,tmp;
char a[];
while (scanf("%s",&a)&&a[]!='')
{
n=;
for (i=;i<strlen(a); i++)
{
n+=a[i]-;
}
printf("%d\n",(n-)%+);
}
return ;
}
hdu_1013_Digital Roots_201310121652的更多相关文章
随机推荐
- LVS上DR和NAT模式的缺陷
引言 相信一般的小公司用的最多的还是DR和NAT模式,关于DR和NAT模式的原理请看看下图,我们先从lvs的DR和NAT模式特性聊聊一些问题. 问题1.lvs的DR模式和NAT模式核心缺陷有哪些? D ...
- 【WIP】Rails Client Side Document
创建: 2017/09/15 更新: 2019/04/14 删除其他语言的表述 更新: 2017/10/14 标题加上[WIP] 引入JavaScrpit/CSS manifesto n. 货单 ...
- [App Store Connect帮助]三、管理 App 和版本(2.7)输入 App 信息:添加 iMessage 信息版 App 的 App 信息
您可以使用 Messages framework(Messages 框架)来创建贴纸包或 iMessage 信息版 App(可在 iMessage App Store 中获取).可作为独立 App,也 ...
- 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]
[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...
- BZOJ 2506 分块
//By SiriusRen #include <bits/stdc++.h> using namespace std; ; ][],g[N],tmp=; struct Node{int ...
- BZOJ 1137 半平面交
半平面交的板子 //By SiriusRen #include <bits/stdc++.h> #define double long double using namespace std ...
- 题解报告:hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 Problem Description 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每 ...
- ex41习题 41: 来自 Percal 25 号行星的哥顿人(Gothons)
ex41习题 41: 来自 Percal 25 号行星的哥顿人(Gothons) 学习到本题卡住了,遇到一点费解的地方,mark一下.本题主要是介绍函数在字典这种数据类型中的应用,本实验在python ...
- Linux-fork()函数详解,附代码注释
// // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...
- 【转】linux下passwd命令设置修改用户密码
1.passwd 简单说明: 我们已经学会如何添加用户了,所以我们还要学习设置或修改用户的密码:passwd命令的用法也很多,我们只选如下的几个参数加以说明:想了解更多,请参考man passwd或p ...