题目1106:数字之和
时间限制:1 秒内存限制:32 兆特殊判题:否提交:873解决:618
题目描述:
对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和。
输入:
每行输入数据包括一个正整数n(0<n<40000),如果n=0 表示输入结束,并不用计算。
输出:
对于每个输入数据,计算其各位数字之和,以及其平方值的数字之和,输出在一行中,之间用一个空格分隔,
但行末不要有空格。
样例输入:
4
12
97
39999
0
样例输出:
4 7
3 9
16 22
39 36
package 吉林大学复试题.第2题_数字之和;
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
Scanner cin = new Scanner(new BufferedInputStream(System.in));
BigInteger bigInteger = null;
int temp, count1, count2;
String str = null;
while (cin.hasNext())
{
count1 = 0;
count2 = 0;
temp = cin.nextInt();
if (temp == 0)
{
return;
}
else
{
str = temp + "";
for (int i = 0; i < str.length(); i++)
{
count1 += str.charAt(i) - 48;
}
// 平方
bigInteger = BigInteger.valueOf(temp);
bigInteger = bigInteger.multiply(bigInteger);
str = bigInteger.toString().trim();
for (int i = 0; i < str.length(); i++)
{
count2 += str.charAt(i) - 48;
}
System.out.println(count1 + " " + count2);
}
}
}
}