栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

杭电1013(JAVA版)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

杭电1013(JAVA版)

Digital Roots   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.   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 24 39 0 Sample Output 6 3

原先的答案是

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()) {
            int num=scanner.nextInt();
            if(num==0) break;
            while(num>=10) {
                int sum=0;
                String strNum=num+"";
                for(int i=0;i 

提示“Wrong Answer”,去结合了其他人的代码发现用的都是BigInteger,修改了代码再测试果然可以了。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()) {
            BigInteger num=scanner.nextBigInteger();
            if(num.compareTo(new BigInteger("0"))==0) break;
            while(num.compareTo(new BigInteger("9"))==1) {
                int sum=0;
                String strNum=num+"";
                for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332034.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号