【杭电】1013【 Digital Roots】 犯下的错误
2015-09-10 21:40
405 查看
Digital Roots
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (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, 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
用字符串来保存数字可以做到每个数字独立,且每个数字范围为0~9,而我一开始的想法就是输入一个数字的字符串数组,我计算所有数字的总和,然后再把这个和sum的每个数字再存入数字的字符串数组,后面的做法就跟前面一样了,而循环的终止条件就是总和sum<10.
首先,这看之下,这题目,太简单了,加法循环跳出就OK了啊
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
while (scanf("%d", &a) ==1 && a)
{
b=0;
while(a)
{
if(a<10)
{
b+=a;a=0;
}
else {b+=a/10;a=a%10;}
if(!a && b>9){a=b;b=0;}
}
printf("%d\n",b);
}
return 0;
}
咔咔咔咔 得 就写完了, 完事还挺高兴的(毕竟初学者嘛) 结果一直报Wrong Answer ,想了半天,才发现 数字会溢出
所以有了下面这个简洁的代码
#include<stdio.h>
int main()
{
int i,m;
char s[1000];
while(scanf("%s",s)==1&&s[0]!='0')
{
for(m=i=0;s[i];i++)
m+=s[i]-'0';
printf("%d\n",m%9==0?9:m%9);
}
return 0;
}
哈哈哈,真是机智!!!!
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (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, 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
用字符串来保存数字可以做到每个数字独立,且每个数字范围为0~9,而我一开始的想法就是输入一个数字的字符串数组,我计算所有数字的总和,然后再把这个和sum的每个数字再存入数字的字符串数组,后面的做法就跟前面一样了,而循环的终止条件就是总和sum<10.
首先,这看之下,这题目,太简单了,加法循环跳出就OK了啊
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
while (scanf("%d", &a) ==1 && a)
{
b=0;
while(a)
{
if(a<10)
{
b+=a;a=0;
}
else {b+=a/10;a=a%10;}
if(!a && b>9){a=b;b=0;}
}
printf("%d\n",b);
}
return 0;
}
咔咔咔咔 得 就写完了, 完事还挺高兴的(毕竟初学者嘛) 结果一直报Wrong Answer ,想了半天,才发现 数字会溢出
所以有了下面这个简洁的代码
#include<stdio.h>
int main()
{
int i,m;
char s[1000];
while(scanf("%s",s)==1&&s[0]!='0')
{
for(m=i=0;s[i];i++)
m+=s[i]-'0';
printf("%d\n",m%9==0?9:m%9);
}
return 0;
}
哈哈哈,真是机智!!!!
相关文章推荐
- Redis学习3之单节点上redis集群搭建和使用
- UITextField
- lcx.exe 转发
- ORM进阶:映射文件编写(上)-基本映射
- 0909关于编译的看法
- Rust - Borrowed pointers | 借贷指针
- Shell脚本-良好的习惯
- 第97讲:使用SBT开发Akka第一个案例环境搭建详解学习笔记
- 设计模式C++实现——工厂模式
- 复制控制(管理指针成员)
- 第二周项目零,宣告“主权”
- iOS的输入事件
- JS 获取某个时间距离现在时间有多少时分秒
- HDU 4183 Pahom on Water(点双连通分量)
- Leet Code 11 Container With Most Water
- Effective C++ 条款38 通过复合塑模出has-a或"根据某物实现出"
- Rust - Unique pointers | 独一无二的指针(所有权指针)
- Hat's Fibonacci 1250 (大数+数学 好题)
- 设计模式——结构型模式
- iOS开发之 KVC,KVO简单了解