算法:Rev(X)函数为翻转数字 输入两个数 求他们的 result=Rev(Rev(num1)+Rev(num2))

时间:2021-07-09 13:20:54
package test;

import java.util.Scanner;

/**
* 算法 Rev(X)函数为翻转数字 输入两个数 求他们的 result=Rev(Rev(num1)+Rev(num2)) 若翻转后存在0,去除
*/

public class Test2{

public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
int str3;
String result;
str3=Rev(str1)+Rev(str2);
result=String.valueOf(str3);
str3 = Rev(result);
System.out.println(str3);


}
static int Rev(String str){
char temp=0;
String newStr="";
char chars[] = str.toCharArray();
for(int i=0;i<chars.length/2;i++){
temp=chars[i];
chars[i] = chars[chars.length-i-1];
chars[chars.length-i-1] = temp;
}
for(int i=0;i<chars.length;i++){
if(chars[i]=='0' && (i==0 || i==chars.length-1)){
continue;
}else {
newStr += chars[i];
}
}
return Integer.parseInt(newStr);
}

}