HDU 1013.Digital Roots【模拟或数论】【8月16】
Digital Roots
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.
24
39
0
6
3
一个数。各个位数相加得到的数假设小于10就输出,否则就继续把得到的数各个位数相加。一看我就模拟做的。模拟的时候要注意。输入的数字可能非常大。所以用int是不能够的,要用字符串处理。模拟做法代码例如以下:
#include<cstdio>
#include<cstring>
void zuo(int x){
int sum=0;
while(x){
sum+=(x%10);
x/=10;
}
if(sum<10) printf("%d\n",sum);
else zuo(sum);
}
int main(){
char s[1010];
while(scanf("%s",s)&&s[0]!='0'){
int x=0;
for(int i=0;i<strlen(s);i++)
x+=(s[i]-'0');
zuo(x);
}
return 0;
}
另一种解法。数论的知识。
数字本身: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 12 22 23 24 25 26 27 28 29 30············
各个位数和: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3·············
你会发现。每9个是一个循环。所以仅仅要对9取余就ok了。代码例如以下:
#include<cstdio>
#include<cstring>
int main(){
char s[1010];
while(scanf("%s",s)&&s[0]!='0'){
int x=0;
for(int i=0;i<strlen(s);i++)
x+=(s[i]-'0');
x=x%9;
if(x==0) printf("9\n");
else printf("%d\n",x);
}
return 0;
}
HDU 1013.Digital Roots【模拟或数论】【8月16】的更多相关文章
- HDU 1013 Digital Roots(to_string的具体运用)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1013 Digital Roots【字符串,水】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1013 Digital Roots(字符串)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013 Digital Roots(字符串,大数,九余数定理)
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1013 Digital Roots 题解
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- hdu 1013 Digital Roots
#include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...
- HDU OJ Digital Roots 题目1013
/*Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1310 - Digital Roots
先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 杭电 1013 Digital Roots
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...
随机推荐
- SQL系统函数的使用(实验五)
SQL系统函数的使用(试验5) 函数在查询语句中的使用 查询员工的姓名和日工资(保留1位小数): 查询并显示部门号为01和02的所有员工的姓名首字及岗位: 查询并显示所有员工的姓名及工龄: 查询199 ...
- C#自动实现Dll(OCX)控件注册的两种方法
尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...
- web前端vertical-align的作用及对象详解
很多程序员知道web前端技术里的vertical-align是什么意思,但是对于vertical-align到底以什么为对齐标准却一知半解,今天我们就来说说web前端vertical-align. 1 ...
- curl安装
问题1: curl: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No su ...
- 2017EIS CTFwriteup
EIS2017也就是2017年高校网络信息安全管理 运维挑战赛,全国一百多所高校参赛,侥幸拿了个地区三等奖,事先不知道理论赛占分比,不然就会是二等奖(吐槽),生活没有如果,下次努力吧. 比赛已经结束大 ...
- Python 面向对象(一) 基础
Python 中一切皆对象 什么是面向对象? 面向对象就是将一些事物的共有特征抽象成类,从类来创建实例. 类class 可以理解为模版 比如人类,都具有身高.体重.年龄.性别.籍贯...等属性,但属性 ...
- lua lua解读
1.线程状态宏定义(thread status) #define LUA_OK 0 #define LUA_YIELD 1 #define LUA_ERRRUN 2 #define LUA_ERRSY ...
- Java数据结构和算法(三)——冒泡、选择、插入排序算法
上一篇博客我们实现的数组结构是无序的,也就是纯粹按照插入顺序进行排列,那么如何进行元素排序,本篇博客我们介绍几种简单的排序算法. 1.冒泡排序 这个名词的由来很好理解,一般河水中的冒泡,水底刚冒出来的 ...
- Less的@import指令
Less的@import指令 Less中,可以通过 @import指令来导入外部文件.@import指令可以放在代码中的任何位置,导入文件时的处理方式取决于文件的扩展名: 如果扩展名是 .css,文件 ...
- ThinkPHP中ajax绑定select下拉框无法显示
html代码: 控制器代码: 其中的<option value="{$vo.gradeId}">{$one.gradeName}</option> 在操作过 ...