hdoj1013_Digital Roots
来源:互联网 发布:grub修复系统引导linux 编辑:程序博客网 时间:2024/06/03 20:42
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.
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
24390
Sample Output
63开始的时候没有考虑大数的问题,后面知道测试数据有大数的时候注意下就可以了#include<iostream>#include<string>using namespace std;short num[10000], tnum[10000];//num存储每一次计算的结果,tnum存储每一次计算的中间结果,数组每一位存储计算结果的一位数字string input;int pos = 0;//当前中间计算结果tnum共有几位,也作为每一次计算结果的位数进行参数传递void solve(int n){pos = 0;if (n==0)//如果计算结果中只有一位数字时,输出结果,结束计算cout << num[0] << endl;else{for (int i = 0; i <= n;i++)//每一数据位数字累加{tnum[0] += num[i];for (int j = 0; j <= pos;j++)//判断是否有进位{if (tnum[j] >= 10){tnum[j + 1] += tnum[j] / 10;tnum[j] = tnum[j] % 10;}elsebreak;}if (tnum[pos + 1] > 0)//计算结果位数又增加一位{pos++;tnum[pos+1]=tnum[pos]/10;tnum[pos] %= 10;}}memcpy(num, tnum, sizeof(num));memset(tnum, 0, sizeof(tnum));solve(pos);//再次递归调用}}int main(int argc, char* argv[]){while (cin >> input&&input[0] != '0'){memset(num, 0, sizeof(num));memset(tnum, 0, sizeof(tnum));for (int i = 0; i < input.size(); i++)num[i] = input[i] - '0';solve(input.size()-1);}return 0;}
0 0
- hdoj1013_Digital Roots
- Digital Roots
- Primitive roots
- Digital Roots
- Digital Roots
- Digital Roots
- ZOJ_1115_Digital Roots
- Digital Roots
- POJ_1284_Primitive Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Digital Roots
- Discrete Roots
- [LeetCode 109]Convert Sorted List to Binary Search Tree
- Android下如何理解onMeasure,onLayout的过程
- 【控件篇】ViewPager+FragmentStatePagerAdapter 页面切换案例详解
- HDU1452 Happy 2004【因数之和】
- iphone鉴权,idps,iPhone蓝牙回控,iphone键盘,iphone手机互联,USB外设开发
- hdoj1013_Digital Roots
- pcb封装1
- 批处理文件夹里的图片
- 索引图像处理的一些心得
- 简单链表的实现
- Java中的大小写字母相互转换(不利用Java自带的方法)
- HDU4313Matrix(用最小生成树思想,集合划分)
- LeetCode - Jump Game II
- JAVA基础知识