还是A+B
问题描述
读入两个小于10000的正整数A和B,计算A+B。需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1。
输入
测试输入包含若干测试用例,每个测试用例占一行,格式为"A B
K",相邻两数字有一个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。
输出
对每个测试用例输出1行,即A+B的值或者是-1。
输入例子 1
2 3 1\n12 22 1\n11 111 2\n0 0 2
输出例子 1
5\n-1\n-1
java版
import java.util.Scanner;
public class Main {
public static int and(int i,int j,int k) {
int sum=i+j;
while(k!=0){
k-=1;
if(i%10==j%10){
i/=10;
j/=10;
}
else
return sum;
}
return -1;
}
public static void main(String[] args) {
int A, B, K;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
A = scanner.nextInt();
B = scanner.nextInt();
K = scanner.nextInt();
if (A == 0 && B == 0)
break;
int sum = and(A, B, K);
System.out.println(sum);
}
}
}
Python版
while True:
A,B,K = map(int,input().split())
if A == 0 and B == 0:
break
c=pow(10,K)
if A%c==B%c:
print("-1")
else:
print(A+B)