一、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.

二、题解

        本来只用数字操作的,后来发现数字太大了。只能用字符串操作了。

三、java代码

import java.util.Scanner;     

  public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String s;
int i,sum;
while(true){
s=cin.next();
if(s.charAt(0)==48)
break;
while(true){
sum=0;
for(i=0;i<s.length();i++){
sum+=s.charAt(i)-48;
}
if(0<sum&&sum<=9){
System.out.println(sum);
break;
}else
s="";
s+=sum;
}
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 1519 Digital Roots (计算根数字)的更多相关文章

  1. POJ 1519 Digital Roots

    题意:求数根. 解法:一个数的数根就是mod9的值,0换成9,只是没想到给的是一个大数……只好先把每位都加起来再mod9…… 代码: #include<stdio.h> #include& ...

  2. POJ 1519:Digital Roots

    Digital Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25766   Accepted: 8621 De ...

  3. HDU 1163 Eddy's digital Roots

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. POJ 1284 Primitive Roots 原根

    题目来源:POJ 1284 Primitive Roots 题意:求奇素数的原根数 思路:一个数n是奇素数才有原根 原根数是n-1的欧拉函数 #include <cstdio> const ...

  5. Digital Roots 1013

    Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:456            测试通过:162 描述 T ...

  6. Eddy's digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏

    Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. ACM——Digital Roots

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...

  9. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

随机推荐

  1. StackOverflow&&Quora&&More 翻译系列——目录

    启动了一个翻译系列,主要收录个人在伯乐在线上翻译的文章,或者在 StackOverflow.Quora 及其他资讯站上发现的好文,选文比较偏个人喜好.希望能够学习.理解文章的同时提高英语水平,并共享知 ...

  2. IO密集型操作时,为什么线程比进程更好?

    在IO密集型的操作时,进程线程都不会太占用CPU,但是进程消耗的资源比较多.

  3. 【android】在Service的onStartCommand()中调用stopself()应该注意的问题

    在Service的onStartCommand()中调用stopself()后并不会立马destroy掉service,而是等onStartCommand()运行完才destroy. public c ...

  4. Django开发模式会加载两次settings文件导致RotatingFileHandlerError

    当使用RotatingFileHandler作为django的日志处理器的时候,会报: Traceback (most recent call last): File "C:\Python2 ...

  5. Codeforces441C_Valera and Tubes(暴力)

    Valera and Tubes time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. hash是什么?

    最近读关于php内核的资料,发现php中 在实现变量以及数据类型的实现中大量使用哈希算法,并且非常细致做出了很多优秀的细节设计.比如:在 zend.hash.h 中 static inline ulo ...

  7. Lean In - 读书笔记

    以下就是摘录了这本书中的相关内容. 他告诉我,想要“赢得每个人的喜欢”这种想法会阻碍我的发展.他说,当你想要让事情有所改变时,你不可能取悦每个人:而如果你去取悦每个人,你就不会获得充分的进步.扎克伯格 ...

  8. Mysql——JDBC编程 简单的例子

    第一类连接Mysql方法见下图: 第二类连接Mysql方法:(跟第一类差不多,并提供查询操作) 首先在Mysql中建立testjdbc数据库,在该数据库下面建立Student表: 参考代码: CREA ...

  9. 20165101 学习基础和C语言基础调查

    学习基础和C语言基础调查 技能学习心得 看了15级学长学姐丰富的技能之后,我感到很惭愧.我的课外技能可以说是很糟糕.唱歌的话,小时候还可以用假声唱一下,变声之后就是高音上不去,低音下不来.体育更是差劲 ...

  10. Android 蓝牙实例【转】

    本文转自:http://www.yiibai.com/android/android_bluetooth.html 在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据. Android平 ...