博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电oj Problem-1013 Digital Roots
阅读量:6914 次
发布时间:2019-06-27

本文共 1868 字,大约阅读时间需要 6 分钟。

Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 65477    Accepted Submission(s): 20417
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
 
#include 
#include
int main(){ char a[10000005]; //数组要开大 int len, len1, k, i; while (scanf("%s", a) != EOF) { len = strlen(a); if (a[0] == '0' && len == 1) break; int temp; int sum = 0; for(i = 0;i < len; i++) sum += a[i] - '0'; while (sum > 9) { k = 1; for (len1 = 1; ; len1++){ k *= 10; if(k > sum) break; } temp = 0; for(i = 0;i < len1; i++){ temp += sum % 10; sum /= 10; } sum = temp; } printf("%d\n", sum); } return 0;}

转载于:https://www.cnblogs.com/cniwoq/p/6770970.html

你可能感兴趣的文章
.Net处理Oracle中Clob类型字段总结
查看>>
当看到某些人月薪十万而觉得郁闷时,看看下面的话
查看>>
五款最佳Linux下载管理器推荐
查看>>
再谈下 Silverlight 跨线程
查看>>
宇瞻U盘出现无法格式化 写保护的完美解决办法 厂家提供的
查看>>
Hadoop概念学习系列之Hadoop的文件系统(十六)
查看>>
C++ 打开exe文件的方法(VS2008)
查看>>
Windows服务安装后自动启动
查看>>
IGT中国
查看>>
Android消息循环分析
查看>>
11. 系统状态管理
查看>>
Java:java+内存分配及变量存储位置的区别
查看>>
PHP 字符串编码的转换
查看>>
往文件中按行写入数据
查看>>
20. Screen
查看>>
整个站点默认禁用 Session,而某个页面不禁用的做法。
查看>>
ios实例开发精品源码文章推荐(8.22)
查看>>
ElasticSearch 应用场景
查看>>
《数据库技术基础与应用(第2版)》学习笔记——第1章
查看>>
Tomcat性能调优方案
查看>>