测试输入包含若干测试用例,每个测试用例占一行,格式为"A B K",相邻两数字有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。
对每个测试用例输出1行,即A+B的值或者是-1。
1 2 1 11 21 1 108 8 2 36 64 3 0 0 1
3 -1 -1 100
#include <iostream> using namespace std; int main() { int a = 0, b = 0, k = 0; while (cin>>a>>b>>k && 0 != a && 0 != b) { int tmpA = a, tmpB = b, ans = -1; while (k--) { if (tmpA % 10 != tmpB % 10) { ans = a + b; break; } tmpA /= 10; tmpB /= 10; } cout << ans << endl; } return 0; }
#include<stdio.h>//末尾k位全相等时输出-1 int main() { int a,b,k,sum,key,aa,bb,i; while(scanf("%d%d%d",&a,&b,&k)!=EOF) { if(a==0&&b==0) break; key=0;//0代表相同 aa=a;bb=b; for(i=1;i<=k;i++)//k个位都进行比较 { if(aa%10==bb%10) { aa/=10;bb/=10; }//上一位 else { key=1;break; }//表示不相同 } if(key==0) sum=-1; else sum=a+b; printf("%d\n",sum); } }
#include<bits/stdc++.h> using namespace std; bool fun(int a,int b,int k){ for(int i=1;i<=k;i++){ if(a%10!=b%10)return true; a/=10; b/=10; } return false; } int main(){ int a,b,k; while(~scanf("%d %d %d",&a,&b,&k)){ if(a==0&&b==0)break; fun(a,b,k)?printf("%d\n",a+b):printf("-1\n"); } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int a = scanner.nextInt(); int b = scanner.nextInt(); int k = scanner.nextInt(); System.out.println((a-b)%(Math.pow(10,k))==0?"-1":a+b); } } }
#include<iostream> using namespace std; int main(){ int a,b,k; while(cin>>a>>b>>k){ if(a==0&&b==0) break; int num1[8]={0},num2[8]={0},count1=0,count2=0,temp1=a,temp2=b; while(temp1>0){//获取a的每一位 num1[count1++]=temp1%10; temp1/=10; } while(temp2>0){//获取b的每一位 num2[count2++]=temp2%10; temp2/=10; } bool judge=1; for(int i=0;i<k;i++)//比较是否前k位是否相同,judge作为输出结果方式判断 if(num1[i]!=num2[i]){ judge=0; break; } if(!judge) cout<<a+b<<endl; else cout<<"-1"<<endl; } }
int main(void){
int a,b,k,tempa,tempb,aa,bb;
while(scanf("%d %d %d",&a,&b,&k) != EOF){
if(a == 0 && b == 0) return 0;
else if(a == 0 && b != 0) printf("%d\n",a+b);
else if(a != 0 && b == 0) printf("%d\n",a+b);
else{
tempa = a;
tempb = b;
while(tempa && tempb && k){
aa = tempa % 10;
bb = tempb % 10;
if(aa != bb) break;
else {
k --;
tempa /= 10;
tempb /= 10;
}
}
}
if(k == 0){
printf("-1\n");
}
else printf("%d\n",a+b);
}
return 0;
}
#include<stdio.h> #include<math.h> int main (){//the shorter,the better. int A,B,K,t;//末尾K位 for(;~scanf("%d%d%d",&A,&B,&K)&&(t=pow(10,K),A||B)&&printf("%d\n",A%t==B%t?-1:A+B);); }
#include<stdio.h> int main (){//the shorter,the better. int A,B,K;char a[4],b[4];//末尾第K位 for(;~scanf("%d%d%d",&A,&B,&K)&&(A||B)&&printf("%d\n",K<=4&&a[sprintf(a,"%04d",A)-K]==b[sprintf(b,"%04d",B)-K]?-1:A+B);); }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); int k = scanner.nextInt(); if (a == 0 && b == 0) { break; } String aString = Integer.toString(a); String bString = Integer.toString(b); String aPost = ""; String bPost = ""; if (k <= aString.length()) { for (int i = aString.length() - k; i < aString.length(); i++) { aPost += Character.toString(aString.charAt(i)); } }else { aPost = Integer.toString(a); } if (k <= bString.length()) { for (int i = bString.length() - k; i < bString.length(); i++) { bPost += Character.toString(bString.charAt(i)); } }else { bPost = Integer.toString(b); } if (aPost.equals(bPost)) { System.out.println(-1); }else { System.out.println(a+b); } } } }
#include <bits/stdc++.h> using namespace std; int main(){ string sa,sb; int k; while(cin>>sa>>sb>>k) { int res; if(sa=="0"&&sb=="0") break; int asize=sa.size(); int bsize=sb.size(); k=min(k,asize); k=min(k,bsize); if(sa.substr(sa.size()-k)==sb.substr(sb.size()-k)) res=-1; else { res=stoi(sa)+stoi(sb); } cout<<res<<endl; } return 0; }
#include <cstdio> #include <cmath> int LastK(int n ,int k){ //求末尾K位 int remain = 0 , K = 0; for(int i = 0; i < k; ++i){ //k次 remain = n%10; n /= 10; K = K + remain*pow(10,i); } return K; } int main(){ int a,b,k; while(scanf("%d %d %d",&a,&b,&k) != EOF){ if(a ==0 & b==0) break; if(LastK(a,k) == LastK(b,k)){ printf("-1\n"); }else{ printf("%d\n",a+b); } } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int a, b, k; while (cin >> a >> b >> k) { if (a == 0 && b == 0) {//如果都是0则结束 break; } int dig = pow(10, k); if (a % dig == b % dig) {//后k位一样,直接比较求余是不是一样就可以了 cout << "-1" << endl; } else { cout << a + b << endl; } } }
#include <iostream> using namespace std; bool check(int a, int b, int k){ while (k-- > 0){ if (a % 10 != b % 10) return false; a /= 10; b /= 10; } return true; } int main() { int a, b, k; while (cin >> a >> b >> k && a != 0 && b != 0){ if (check(a, b, k)){ cout << -1 << endl; continue; } cout << a + b << endl; } }
#include<bits/stdc++.h> using namespace std; int main() { int n, m, k; while (cin >> n >> m >> k) { if (n == 0 && m == 0) { break; } int temp_n = n, temp_m = m; int flag = 1; while (k--) { int ansn = temp_n % 10; int ansm = temp_m % 10; if (ansn != ansm) { flag = 0; break; } temp_n /= 10; temp_m /= 10; } if (flag == 1) { cout << -1 << endl; } else { cout << n + m << endl; } } return 0; }