12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。
import java.io.*; import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; 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) { //录入 String[] s = str.split(" "); int len = s.length; //转成int数组 int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = Integer.parseInt(s[i]); } int sum1 = a[0] + a[1]; StringBuilder temps = new StringBuilder(String.valueOf(sum1)); sum1 = Integer.parseInt(String.valueOf(temps.reverse()));//和的反转 //转成int数组,反转 int[] b = new int[len]; for (int i = 0; i < len; i++) { StringBuilder sb = new StringBuilder(s[i]); b[i] = Integer.parseInt(String.valueOf(sb.reverse())); } int sum2 = b[0] + b[1];//反转的和 if (sum1 == sum2) System.out.println(a[0]+a[1]); else System.out.println("NO"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ // 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转。 int i = scanner.nextInt(); //12 int j = scanner.nextInt(); ///34 int i1 = getReverse(i); //21 int i2 = getReverse(j); //43 int sum1 = i+j; //46 int sum2 = i1+i2; //64 System.out.println(getReverse(sum1)==sum2?sum1:"NO"); } } static int getReverse(int i){ return Integer.parseInt(new StringBuilder(String.valueOf(i)).reverse().toString()); } }