一、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. linux shell脚本: 自动监控网站状态并发送提醒邮件

    1.创建监控脚本:$ vi /alidata/shell/webcheck.sh #!/bin/sh weblist="/alidata/shell/weblist.txt" my ...

  2. [转]How Hash Algorithms Work

    来看看SHA-1到底是如何工作的 http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software

  3. 安装Eclipsemaven插件

    Maven的Eclipse插件m2eclipse的安装 The goal of the m2ec project is to provide a first-class Apache Maven su ...

  4. git克隆远程分支

    $ git branch –r 查看远程branch信息 $ git checkout origin/dev检出远程分支 $ git branch -a 查看所有分支,包括本地和远程 可以使用chec ...

  5. linux c编程:进程控制(二)_竞争条件

    前面介绍了父子进程,如果当多个进程企图对共享数据进行处理.而最后的结果又取决于进程运行的顺序时,就认为发生了竞争关系.通过下面的例子来看下 在这里标准输出被设置为不带缓冲的,于是父子进程每输出一个字符 ...

  6. 数据库存储I/O类型分析与配置

    存储设备作为数据的容器,为应用提供数据存取服务,而存储系统将数据展现给不同的应用后,应用程序对数据访问不尽相同.简要来说,就是读和写,更加细分的话是以不同的传输单元(I/O大小)进行顺序和随机类型的读 ...

  7. 让input表单输入框不记录输入过信息的方法

    有过表单设计经验的朋友肯定知道,当我们在浏览器中输入表单信息的时候,往往input文本输入框会记录下之前提交表单的信息,以后每次只要双击input文本输入框就会出现之前输入的文本,这样有时会觉得比较方 ...

  8. tp导出excel

    //数据导出 protected function dao($db,$where,$join,$field){ $data = M($db)->join($join)->where($wh ...

  9. 算法(Algorithms)第4版 练习 1.3.4

    主要思路: 遇到左括号则一直压栈,遇到右括号时则从栈中弹出一个元素. 如果此时栈为空,则返回false. 如果这个元素与右括号不匹配,则返回false. 重复此过程,最后判断栈是否为空,若为空则返回t ...

  10. Win7远程桌面_ZC01

    1.Win7的远程桌面,在一开始设置成全屏显示,当远程桌面窗口 切换为非全屏的状态时,要想再切换成全屏状态 就比较困难了(我的笔记本ThinkpadE440一直没有弄成功...) 1.1.网上搜索各种 ...