题解 | #数字翻转#
数字翻转
https://www.nowcoder.com/practice/bc62febdd1034a73a62224affe8bddf2
import java.util.Scanner; // import java.util.splitIntoDigits; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); Deque<Integer> digits1 = splitIntoDigits(a); Deque<Integer> digits2 = splitIntoDigits(b); int sum1 = 0; int m1 = digits1.size()-1; while(!digits1.isEmpty()){ sum1 += digits1.remove()*Math.pow(10,m1); m1--; } int sum2 = 0; int m2 = digits2.size()-1; while(!digits2.isEmpty()){ sum2 += digits2.remove()*Math.pow(10,m2); m2--; } int c = sum1+sum2; //对rev(x) + rev(y)进行翻转 Deque<Integer> digits3 = splitIntoDigits(c); int sum3 = 0; int m3 = digits3.size()-1; while(!digits3.isEmpty()){ sum3 += digits3.remove()*Math.pow(10,m3); m3--; } System.out.println((sum3)); } public static Deque<Integer> splitIntoDigits(int num){ Deque<Integer> deque = new LinkedList<>(); while(num>0){ deque.add(num % 10);//余数. 取余数即可 num /= 10; //除以10的结果512/10=51 } // 123 -> 321 100 -> 001 peek消除 while(deque.peek()==0){ deque.remove(deque.peek()); } //消除前导0 return deque; } }