HDOJ 1013 Digital Roots

来源:互联网 发布:豆瓣python 编辑:程序博客网 时间:2024/05/22 00:19

HDACM1013
大数解法

import java.math.BigInteger;import java.util.Scanner;public class Main{    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while (sc.hasNext()) {            BigInteger n = sc.nextBigInteger();            if (n.compareTo(BigInteger.ZERO)==0) {                break;            }            BigInteger sum = BigInteger.ZERO;            while (n.compareTo(BigInteger.ZERO)!=0){                BigInteger temp = n.mod(BigInteger.TEN);                sum = sum.add(temp);                n=n.divide(BigInteger.TEN);                if (n.compareTo(BigInteger.ZERO)==0) {                    if (sum.compareTo(BigInteger.TEN)==-1) {                        break;                    }                    n = sum;                    sum = BigInteger.ZERO;                }            }            System.out.println(sum);        }    }}