ZOJ Problem Set

来源:互联网 发布:java手游 编辑:程序博客网 时间:2024/06/02 06:50
Digital Roots

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Background

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.


Example

Input


24390
Output
63
解题报告:
这个题目的意思是,已知有个数字,把各位的数字加起来,如果和>=10,那么重复这个操作,直至为个位数。
如果直接用int会WR,如果用unsigned int会超时;
这个题最好的办法是用字符串;
看了下别人的思路,发现用字符串求数字根有个特殊的方法——合九法
合九法:一个数的数字根等于这个数模9,也等于各个位所有数之和模9
AC代码:
#include<iostream>#include<cstdio>#include<string.h>using namespace std;int main(){char num[1000];int len,sum,i;while(~scanf("%s",&num)){if(num[0]=='0')break;len=strlen(num);if(len==1&&num[0]==0)return 0;for(sum=0,i=0;i<len;i++){sum=sum+num[i]-'0';}printf("%d\n",sum%9?sum%9:9);}return 0;}