ZOJ Problem Set
来源:互联网 发布:java手游 编辑:程序博客网 时间:2024/06/02 06:50
Digital Roots
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
Output63
解题报告:
这个题目的意思是,已知有个数字,把各位的数字加起来,如果和>=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;}
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1151
- ZOJ Problem Set - 1205
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1029
- ZOJ Problem Set - 1076
- ZOJ Problem Set - 1117
- ZOJ Problem Set - 1041
- ZOJ Problem Set - 1004
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1016
- SpringMVC重要接口(三)HttpMessageConverter
- NDK/JNI二维数组多维数组传递
- jQuery入门学习
- FAFU OJ TeX括号
- java 读写 yaml 文件
- ZOJ Problem Set
- 项目中一些细节东西
- Vue下拉框点击选中,点击其他区域消失
- fastclick使用与解密
- 做自媒体博客对几个细节进行优化对推广运营工作好处多多
- MaterialDialog无法设置字体颜色
- 编码读取EXCEL的几点注意
- Android线程管理(一)——线程通信(转载)
- Kaldi语音识别学习笔记(一)