UVa 11332 Summing Digits (water ver.)
2013-11-07 19:21
344 查看
11332 - Summing Digits
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2307
For a positive integer
n, let
f(n)denote the sum of the digits of
nwhen represented in base 10. It is easy to see that the sequence of numbers
n, f(n), f(f(n)), f(f(f(n))), ...eventually becomes a single digit number that repeats forever. Let this single digit be denoted
g(n).
For example, consider
n = 1234567892. Then:
f(n) = 1+2+3+4+5+6+7+8+9+2 = 47 f(f(n)) = 4+7 = 11 f(f(f(n))) = 1+1 = 2
Therefore,
g(1234567892) = 2.
Each line of input contains a single positive integer
nat most 2,000,000,000. For each such integer, you are to output a single line containing
g(n). Input is terminated by
n = 0which should not be processed.
Sample input
2 11 47 1234567892 0
Output for sample input
2 2 2 2
完整代码:
/*0.016s*/ #include<cstdio> #include<cstring> char n[15]; int main() { int i, sum, len; while (gets(n), n[0] != '0') { sum = 0; len = strlen(n); for (i = 0; i < len; ++i) sum += n[i] & 15; sum = sum / 10 + sum % 10; sum = sum / 10 + sum % 10; printf("%d\n", sum); } return 0; }
相关文章推荐
- Redhat/CentOS6.2 x86系统KVM虚拟机网络配置(二)—Bridge方式
- 韩国vieworks
- Redhat/CentOS6.2 x86系统KVM虚拟机网络配置(一)—NAT方式
- Linux分区
- Linux下调试段错误的方法[Segmentation Fault]
- 五个小球完全弹性碰撞
- 增加难度的抽签问题(最内层使用二分法)
- What does grep stand for in Linux operating system?
- hdu 1544 水题
- php应用数据库连接中的单例模式
- 【Android】菜单功能的实现:使用Java代码
- 将java源码打成jar包的两种方法
- WordPress Think Responsive Themes ‘upload_settings_image.php’任意文件上传漏洞
- DELL-linux-风扇
- 有关爬虫的
- 如何设置ubuntu和redhat网络接口
- 长英文自动换行的最终解决方法
- C语言结构体
- C#中 托管资源和非托管资源
- hdu2087