HDOJ 1013
时间:2022-04-22
本文章向大家介绍HDOJ 1013,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30962 Accepted Submission(s): 9508
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
刚开始忽略的数的大小,代码AC
1 #include <iostream>
2 using namespace std;
3 void sum(int n);
4 int main()
5 {
6 int n;
7 while(cin>>n)
8 {
9 if(n == 0)
10 break;
11 else
12 {
13 sum(n);
14 }
15 }
16 return 0;
17 }
18 void sum(int n)
19 {
20 int count=0;
21 while(n>=10)
22 {
23 count+=n%10;
24 n=n/10;
25 }
26 count+=n;
27 if(count>10)
28 sum(count);
29 else
30 cout<<count<<endl;
31 }
后来改用按位来做....需要判断是否为0时候,要注意10这种数,最后一位为0也需要判断...因此改用sum是否为0来确定,是否是第一个数字为0
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 char c=' ';
6 int n,sum=0;
7 while(c=getchar())
8 {
9 if(sum>=10)
10 sum=sum%10+sum/10;
11 if(c == 'n')
12 {
13 if(sum==0)
14 break;
15 cout<<sum<<endl;
16 sum=0;
17 }
18 else
19 {
20 n=c-48;
21 sum+=n;
22 }
23 }
24 return 0;
25 }
- Web前端知识体系精简——CSS 篇
- 几种异步操作方式
- 今天的面试小记
- objective-C中的序列化(serialize)与反序列化(deserialize)
- 微信小程序 template 模板功能实现循环
- ASP.NET Web API 接口执行时间监控
- 这么多前端优化点你都记得住吗?
- 愚蠢的”记住“密码方式终于还是出了问题
- C#获取本机可用端口
- 想知道你的对象用微信和谁聊得最多吗?一个代码告诉你
- objective-C中的接口与泛型
- 段永朝:天性,互联网的灵性回归
- objective-C中的Class(类类型),Selector(选择器SEL),函数指针(IMP)
- 数据中心进水了怎么办?数据中心如何防洪?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- LeetCode题目27:移出元素
- LeetCode题目28:实现strStr()
- LeetCode题目29:两数相除
- LeetCode题目30:串联所有单词的子串
- LeetCode题目31:下一个排列
- LeetCode题目33:搜索旋转排序数组
- LeetCode题目34:在排序数组中查找元素的第一个和最后一个位置
- LeetCode题目35:搜索插入位置
- LeetCode题目36:有效的数独
- 你必须掌握动态规划——LeetCode题目5:最长回文子串
- 有意思的难题——LeetCode题目37:解数独
- 源码分析-分布式链路追踪:Skywalking存储插件能力-elasticsearch
- mongodb 4.0副本集搭建
- 浅析Kubernetes Pod重启策略和健康检查
- SpringBoot2 整合Ehcache组件,轻量级缓存管理