九度oj 1124
2015-07-27 13:31
169 查看
题目描述:
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.
输入:
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.
输出:
For each integer in the input, output its digital root on a separate line of the output.
样例输入:
样例输出:
#include<stdio.h>
int total(int n)
{
int a = n / 10,b = n % 10,result = b;
while(a != 0)
{
b = a % 10;
a /= 10;
result += b;
}
return result;
}
int main()
{
int n;
char ch;
while((ch = getchar()) != '0')
{
n = ch - '0';
while((ch = getchar()) != '\n') n += (ch - '0');
while(n >= 10)
n = total(n);
printf("%d\n",n);
}
return 0;
}
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.
输入:
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.
输出:
For each integer in the input, output its digital root on a separate line of the output.
样例输入:
24 39 0
样例输出:
63
#include<stdio.h>
int total(int n)
{
int a = n / 10,b = n % 10,result = b;
while(a != 0)
{
b = a % 10;
a /= 10;
result += b;
}
return result;
}
int main()
{
int n;
char ch;
while((ch = getchar()) != '0')
{
n = ch - '0';
while((ch = getchar()) != '\n') n += (ch - '0');
while(n >= 10)
n = total(n);
printf("%d\n",n);
}
return 0;
}
相关文章推荐
- linux初学(八)之使用fdisk进行磁盘管理
- poj 2251 Dungeon Master(三维BFS)(中等)
- [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
- 【leetcode-16】3Sum Closest(java)
- 提高ios开发效率
- _CrtIsValidHeapPointer 跨dll接口参数数据传递问题
- 7.27计划
- javascript中的闭包
- Poj 1201 差分约束问题 详解
- hdu 3008 Warcraft
- HDU 5074 Hatsune Miku (动态规划)
- Spark Streaming 流计算优化记录(1)-背景介绍
- 2.朴素贝叶斯Naive Bayes
- mac shell终端编辑命令行快捷键——行首行尾
- 随机数
- c++获取系统d当前时间
- Linux文件的软链接和硬链接
- hdu 5312 Sequence
- Java取得指定年月的最大天数
- C++小品:井水不犯河水的thread_specific_ptr,C++11线程库中的本地存储