HDOJ-ACM1013(JAVA)

时间:2022-07-20 15:37:26

HDOJ-ACM1013(JAVA)

这道题也很简单~主要是将输入转为字符串(存储大数,操作字符数组)

题意:

24 : 2 + 4 = 6  输出6

39 : 3 + 9 = 12 , 1 + 2 = 3 输出3

999: 9 + 9 + 9 = 27 , 2 + 7 = 9 输出9

Java代码如下:

import java.util.*;

import java.io.*;

public class Main{

    public static void main(String[] arg){
Scanner scan = new Scanner(new BufferedInputStream(System.in));
String s;
while(true){
s=scan.next();
char[] cs = s.toCharArray();
if(cs[0] == '0'){
break;
}
if(cs.length==1){
System.out.println(s);
continue;
}
System.out.println(getSum(cs));
}
scan.close();
} static int getSum(char[] cs){
int sum = 0;
while(cs.length!=1){
sum = 0;
for(int i = 0 ; i != cs.length ; i ++ ){
sum = cs[i]-'0' + sum;
}
cs = String.valueOf(sum).toCharArray();
}
return sum;
}
}