输入为一行,x、y(1 ≤ x、y ≤ 1000),以空格隔开。
输出rev(rev(x) + rev(y))的值
123 100
223
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int x=sc.nextInt(); int y=sc.nextInt(); int n1=rev(x); int n2=rev(y); System.out.println(rev(n1+n2)); } } public static int rev(int n){ int x=0,tmp=n; while(n!=0){ x=x*10+n%10; n/=10; } return x; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNextInt()) { int a=scanner.nextInt(); int b=scanner.nextInt(); int c=rev(a)+rev(b); int d=rev(c); System.out.println(d); } } public static int rev(int n) { String str=String.valueOf(n); StringBuilder sBuilder=new StringBuilder(); for(int i=str.length()-1;i>=0;i--) { char s=str.charAt(i); sBuilder.append(""+s); } String str2=sBuilder.toString(); int result=Integer.parseInt(str2); return result; } }
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
while(reader.hasNext()){
int sum;
String str=reader.nextLine();
List<Integer> ilist=new ArrayList<>();
List<String> list = Arrays.stream(str.split(" ")).collect(Collectors.toList());
list.stream().forEach(item->{
item=new StringBuffer(item).reverse().toString();
ilist.add(Integer.valueOf(item));
});
sum = ilist.stream().mapToInt(i -> i).sum();
String strs=String.valueOf(sum);
System.out.println(Integer.valueOf(new StringBuffer(strs).reverse().toString()));
}
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
StringBuffer sbx=new StringBuffer();
sbx.append(x+"");
String strx=sbx.reverse().toString();
StringBuffer sby=new StringBuffer();
sby.append(y+"");
String stry=sby.reverse().toString();
int sum=Integer.parseInt(String.valueOf(strx))+Integer.parseInt(String.valueOf(stry));
StringBuffer sbsum=new StringBuffer();
sbsum.append(sum+"");
String s=sbsum.reverse().toString();
for(int i=0;i<s.length();i++) {
if(Integer.parseInt(String.valueOf(s.charAt(i)))!=0) {
System.out.println(s.substring(i,sbsum.reverse().toString().length()));
break;
}else {
continue;
}
}
}
}
大爱StringBuffer
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println(rev(rev(x) + rev(y))); } public static int rev(int n) { String s = String.valueOf(n); StringBuilder sb = new StringBuilder(s); sb = sb.reverse(); for (int i = 0;i < sb.length();i ++) { if (sb.charAt(i) == '0') { sb.deleteCharAt(i); }else break; } return Integer.valueOf(sb.toString()); } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub //System.out.println("hello world"); Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); String aString=String.valueOf(a); String bString=String.valueOf(b); aString=reverse(aString); bString=reverse(bString); int answer=Integer.valueOf(aString)+Integer.valueOf(bString); if(answer%10==0) { while (answer%10==0) { answer=answer/10; } } String string=String.valueOf(answer); string=reverse(string); System.out.println(string); } private static String reverse(String aString) { char c[]=aString.toCharArray(); int l=0,r=aString.length()-1; char temp; while (l<=r) { temp=c[l]; c[l]=c[r]; c[r]=temp; l++; r--; } return new String(c); } }
数字翻转,余数和整除的应用
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.println(rev(rev(x) + rev(y)));
}
public static int rev(int x) {
int result = 0;
while(x != 0) {
result = x % 10 + result * 10;
x = x / 10;
}
return result;
}
}
用BufferedReader读取控制台输入,效率提高很多,以后不要用Scanner
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null){
String[] strs = str.trim().split(" ");
int x = Integer.parseInt(strs[0]);
int y = Integer.parseInt(strs[1]);
System.out.println(rev(rev(x) + rev(y)));
}
}
public static int rev(int x) {
int result = 0;
while(x != 0) {
result = x % 10 + result * 10;
x = x / 10;
}
return result;
}
}
//解法一 import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num1 = in.nextInt(); int num2 = in.nextInt(); System.out.println(reverse(reverse(num1) + reverse(num2))); } } private static int reverse(int num) { return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString()); } } ------------------------------------------------------------------------- //解法二 import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num1 = in.nextInt(); int num2 = in.nextInt(); System.out.println(reverse(reverse(num1) + reverse(num2))); } } private static int reverse(int num) { int res = 0; while (num > 0) { res = res * 10 + num % 10; num /= 10; } return res; } }
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String[] xy = scanner.nextLine().split(" "); int x = Integer.parseInt(xy[0]); int y = Integer.parseInt(xy[1]); System.out.println(rev(rev(x) + rev(y))); } } private static int rev(int x) { StringBuffer sb = new StringBuffer(); while (x >= 10) { sb.append(x % 10); x = x / 10; } sb.append(x); return Integer.parseInt(sb.toString()); } }这道题还是很简单,重要的是需要搞清楚那个反转函数的功能,感觉就还是把每个数字拿出来反转一下而已
import java.util.Scanner; public class Main { public static int rev(int x){ int count=0; int sum=0,temp,number=0; do{ sum=sum*10; temp = x/10; count=x-(temp)*10; sum=sum+count; x=temp; }while(x>0); return sum; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int x=in.nextInt(); int y=in.nextInt(); System.out.println(rev(rev(x) + rev(y))); } }