Algorithm training--inverted numbers (Java)

Problem Description

  The "reverse number" of an integer refers to another integer obtained by reversing the order of each digit of the integer. If an integer ends with 0, then the 0's are omitted from its inverse. For example, the inverse of 1245 is 5421, and the inverse of 1200 is 21. Please write a program that inputs two integers, then calculates the sum of the inverses of the two integers, and then prints out the inverse of sum. Requirement: Since in this question it is necessary to calculate the inverse number of an integer multiple times, this part of the code must be abstracted into the form of a function.
  Input format: The input is only one line, including two integers, separated by spaces.
  Output format: The output is only one line, which is the corresponding result.
  Input and output samples

Sample input

435 754

Sample output

199

package asf;

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
    public static void main(String [] args) {
        
        Scanner scanner =new Scanner(System.in);
        String strNum1=scanner.next();
        String strNum2=scanner.next();
        
        StringBuffer stringBuffer1=new StringBuffer(strNum1);
        StringBuffer stringBuffer2=new StringBuffer(strNum2);
        
        strNum1=new  String (stringBuffer1.reverse());
        strNum2=new  String (stringBuffer2.reverse());
        
        Integer number1=Integer.parseInt(strNum1);
        Integer number2=Integer.parseInt(strNum2);
        
        int sum=number1+number2;
        
        String sumString=sum+"";
        StringBuffer stringBuffer=new StringBuffer(sumString);
        System.out.print(Integer.parseInt(new  String (stringBuffer.reverse())));


        
        }    
    
    }

Guess you like

Origin blog.csdn.net/qq_62994974/article/details/128614903