#include<iostream> #include<cmath> using namespace std; int Count(int num) { if(num<=9) return num; int sum=0; while(num>0) { sum+=num%10; num/=10; } return sum; } int main() { int number; while(cin>>number) { if(number==0) break; cout<<Count(number)<<" "<<Count(number*number)<<endl; } return 0; }
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.hasNextInt()){ int n=sc.nextInt(); int n1=add(n); int n2=add((int) Math.pow(n,2)); System.out.println(n1+" "+n2); } sc.close(); } public static int add(int n){ String s=String.valueOf(n);//将数字n换成字符串 int sum=0; for(int i=0;i<s.length();i++){ //对于此字符串来说,要先提取出来它的每一位(这里是char类型需要先转换为String类型)转换成十进制数字,然后将字符串的每一位进行数字相加 int x=Integer.parseInt(String.valueOf((s.charAt(i)))); sum+=x; } return sum; } }
#include<iostream> using namespace std; int main(){ int n; while(cin>>n){ int temp=n,sum1=0,sum2=0; while(temp>0){ sum1+=temp%10; temp/=10; } temp=n*n; while(temp>0){ sum2+=temp%10; temp/=10; } cout<<sum1<<" "<<sum2<<endl; } }
//感觉自己弄麻烦了,将输入转化成字符串,然后再用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; } }
#include<iostream> using namespace std; int main(){ int n; cin >> n; while (n != 0){ int result1=0, result2=0; int squara = n*n; while (n / 10 != 0){ result1 += n % 10; n /= 10; } result1 += n; while (squara / 10 != 0){ result2 += squara % 10; squara /= 10; } result2 += squara; cout << result1 << " " << result2 << endl; cin >> n; } }
import java.util.Scanner;
public class Main{
public static void
main(String[] s){
Scanner sc=new
Scanner(System.in);
while(sc.hasNext()){
int
a=sc.nextInt();
if(a==0){
break;
}
test(a);
}
}
public static
void test(int a){
//先考虑输出各位数字之和
int
sum1=0,sum2=0;
int temp=a;
while(temp>0){
sum1+=temp%10;
temp/=10;
}
System.out.print(sum1+" ");
//在考虑它的平方的数的各位和
int b=a*a;
while(b>0){
sum2+=b%10;
b/=10;
}
System.out.println(sum2);
}
}
#include<stdio.h> int main (){//the shorter,the better. int n,t,r1,r2; for(;~scanf("%d",&n)&&(t=n*n);) for(r1=0,r2=0;(n>0&&~(r1+=n%10))+(t>0&&~(r2+=t%10));n/=10,t/=10,!n&&!t?printf("%d %d\n",r1,r2):0); }
int main() { int s,n,n1,n2,temp; while(cin>>n) { int sum1=0,sum2=0; if(n==0) { break; //注意这里,当输入为0时,直接退出,免得输出0 0, //因为当输入数字n为时,是不输入任何值的 } temp=n; n1=temp; while(n1) { s=n1%10; sum1+=s; n1=n1/10; } n2=temp; n2*=n2; while(n2) { s=n2%10; sum2+=s; n2=n2/10; } cout<<sum1<<" "<<sum2<<endl; } return 0; }
#include<stdio.h> using namespace std; int out(long long n){ int acc=0; while(n!=0){ acc=acc+n%10; n=n/10; } return acc; } int main() { long long n; while(scanf("%lld",&n)!=EOF){ long long n2=n*n; printf("%d ",out(n)); printf("%d\n",out(n2)); } }
#include <iostream> using namespace std; int main() { int x; while(cin>>x) { int num=0; int x2=x*x; while(x!=0) { num+=(x%10); x/=10; } cout<<num<<' '; num=0; while(x2!=0) { num+=(x2%10); x2/=10; } cout<<num<<endl; } }