测试输入包含若干测试用例,每个测试用例占一行,格式为"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
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); } } } }
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); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int A = in.nextInt(); int B = in.nextInt(); // 当A和B同时为0时输入结束 if (A == 0 && B == 0) return; int K = in.nextInt(); int M = (int) Math.pow(10, K); if ( (A % M) == (B % M) ) // 如果A和B的末尾K位数字相同 System.out.println(-1); else System.out.println(A + B); } }