import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { int len = str.length(); int sum1 = 0; //原来每个位上数字和 for (int i = 0; i < len; i++) { sum1 += Integer.parseInt(String.valueOf(str.charAt(i))); } int sum2 = 0; //平方后每个位上数字和 BigInteger x = BigInteger.valueOf(Long.parseLong(str)); String xs = String.valueOf(x.multiply(x)); int length = xs.length(); for (int i = 0; i < length; i++) { sum2 += Integer.parseInt(String.valueOf(xs.charAt(i))); } System.out.println(sum1 + " " + sum2); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { //int 可以表示的最大数 2147483647 //1600000000 可以表示 Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int i = scanner.nextInt(); int j =i*i; int sum1=0; while (i>0){ sum1+= i%10; i=i/10; } int sum2=0; while (j>0){ sum2+= j%10; j=j/10; } System.out.println(sum1+" "+sum2); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int n2 = n*n; int sum = 0; while(n>=1){ sum+=n%10; n=n/10; } System.out.print(sum+" "); sum=0; while(n2>=1){ sum+=n2%10; n2=n2/10; } System.out.println(sum); } } }
//感觉自己弄麻烦了,将输入转化成字符串,然后再用substring切割字符串, //将单个字符串转化成数字,再进行计算 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = a*a; System.out.print(add(a)); System.out.print(" " + add(b)); } public static int add(int a) { int sum=0; int c=0; String b=""; String s = String.valueOf(a); for(int i=0; i<=s.length()-1;i++) { b = s.substring(i,i+1); c = Integer.parseInt(b); sum = sum + c; } return sum; } } //看了前面的回答,修改了一下 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = a*a; System.out.print(add1(a)); System.out.print(" " + add1(b)); } public static int add1(int a) { int sum=0; for(;a != 0;) { sum = sum + a % 10; a = a/10; } return sum; } }
都在代码里头了。
import java.util.Scanner;
/**
* @author Allen_Hua
* @create_time 创建时间:May 12, 2018 4:18:03 PM 类说明
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
long n = scan.nextLong();
long n2 = n * n;
System.out.print(getSum(n) + " ");
System.out.println(getSum(n2));
}
}
/**
* 传入一个数,将其分解为一个一个的整数
* 按位相加得到和
**/
private static int getSum(long n) {
// TODO Auto-generated method stub
int sum = 0;
String str = String.valueOf(n);
for (int i = 0; i < str.length(); i++) {
sum += Integer.valueOf(str.charAt(i)) - 48;
}
return sum;
}
}
import java.io.EOFException; import java.util.Scanner; public class Main { public static void main(String[] args) { int n; Scanner in=new Scanner(System.in); while (in.hasNext()){ n=in.nextInt(); String str=Integer.toString(n); char[] s=str.toCharArray(); int sum1=0; int sum2=0; int len=str.length(); for(int i=0;i<len;i++){ sum1+=(int)s[i]-48; } n=n*n; str=Integer.toString(n); s=str.toCharArray(); len=str.length(); for(int i=0;i<len;i++){ sum2+=(int)s[i]-48; } System.out.println(sum1+" "+sum2); } } }
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int[] number=new int[100];
Scanner in=new Scanner(System.in);
int count=0;
number[count]=in.nextInt();
while(number[count]!=0){
number[++count]=in.nextInt();
}
int[] sum=new int[count];
int[] sums=new int[count];
for(int i=0;i<count;i++){
int n=number[i];
sum[i]=fun(n);
sums[i]=fun(n*n);
}
for(int i=0;i<count;i++){
System.out.println(sum[i]+" "+sums[i]);
}
}
public static int fun(int number){
int sum=0;
while(number!=0){
sum+=number%10;
number/=10;
}
return sum;
}
}