对于一个整数X,定义操作rev(X)为将X按数位翻转过来,并且去除掉前导0。例如:
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
现在给出整数x和y,要求rev(rev(x) + rev(y))为多少?
输入为一行,x、y(1 ≤ x、y ≤ 1000),以空格隔开。
输出rev(rev(x) + rev(y))的值
123 100
223
Integer.parseInt(str)方***自动帮我们去除所有的前导0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String x = sc.next();
String y = sc.next();
int xy = rev(x) + rev(y);
System.out.println(rev(String.valueOf(xy)));
}
sc.close();
}
private static int rev(String s) {
String str = new StringBuilder(s).reverse().toString();
return Integer.parseInt(str);
}
}
import java.util.Scanner; public class Main { //数字反转 public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); int y = in.nextInt(); String xstr = String.valueOf(x); String ystr = String.valueOf(y); x = Integer.valueOf(reverse(xstr)); y = Integer.valueOf(reverse(ystr)); int result = x + y; System.out.println(reverse(String.valueOf(result))); } public static String reverse(String num){ int len = num.length(); int ceil = 1; int rx = 0; for(int i=0; i<len; i++){ rx += Integer.parseInt(num.charAt(i)+"") * ceil; ceil *= 10; } return String.valueOf(rx); } }
#include<stdio.h> #include<math.h> int rev(int x) { int ws=0,x1,x2,i=0,j=0,sum=0,N=0; x1=x; x2=x1; //判断位数 while(x1>=1) { x1/=10; ws++; } //数组对应每个位数 int WS[ws]; while(x2) { WS[i]=x2%10; x2/=10; i++; } while(!WS[j]) { j++; } for(i=j;i<=ws-1;i++) { N=pow(10,ws-i-1); sum+=WS[i]*N; } return sum; } int main() { int x,y,result1,result2,total=0; scanf("%d %d",&x,&y); result1=rev(x); result2=rev(y); total+=result1+result2; printf("%d",rev(total)); return 0; }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int x=s.nextInt();
int y=s.nextInt();
System.out.print(rev(rev(x)+rev(y)));
}
public static int rev(int i)
{
int a []=new int[20];
int j=0;
int radix=1;
while(i/radix>0)
{
radix=radix*10;
}
radix=radix/10;
int k=0;
while(radix>0)
{
k=i/radix;
a[j++]=k;
i=i-radix*k;
radix=radix/10;
}
String s="";
for(int ii=a.length-1;ii>=0;ii--)
{
s=s+a[ii];
}
int num=Integer.parseInt(s);
return num;
}
}
import java.util.Scanner; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); //System.out.println("请输入两个整数:"); int x=sc.nextInt(); int y=sc.nextInt(); System.out.println(rever(rever(x)+rever(y))); } public static int rever(int x){ StringBuilder sb=new StringBuilder(String.valueOf(x)); String s=sb.reverse().toString(); while(s.startsWith("0")){ s=s.substring(1); } return Integer.parseInt(s); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x =input.nextInt(); int y =input.nextInt(); System.out.println(Rev(Rev(x)+Rev(y))); } public static int Rev(Integer number) { StringBuilder sb = new StringBuilder(String.valueOf(number)); sb.reverse(); return Integer.valueOf(sb.toString()); } }
Python大法好x,y = input().split(" ") add = int(x[::-1])+int(y[::-1]) add = str(add)[::-1] for i in range(0,len(add)): if(add[i] == '0'): i = i+1 break print(add[i:])
想了想感觉不用判定去首0,那样太束缚Python的灵活了,所以下面有个更简短版的:
x,y = input().split(" ")
add = int(x[::-1])+int(y[::-1])
print(int(str(add)[::-1]))
但是我好奇地对比了一下,第一种方法的运行时间和空间都要优于虽然看上去代码更简短的这种。
//利用StringBuilder自带的翻转函数,最后用Integer.parseInt()去前导0.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
String[] s = line.split(" ");
String s1 = new StringBuilder(s[0]).reverse().toString();
String s2 = new StringBuilder(s[1]).reverse().toString();
int x = Integer.parseInt(s1), y = Integer.parseInt(s2);
int z = x + y;
String res = new StringBuilder(String.valueOf(z)).reverse().toString();
System.out.println(Integer.parseInt(res));
#include <string> #include <iostream> #include <algorithm> #include <cstdlib> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int n = atoi(s1.c_str()) + atoi(s2.c_str()); s1 = to_string(n); reverse(s1.begin(), s1.end()); cout << atoi(s1.c_str()); return 0; }
package LineCode.Recruit2017.网易;
import java.util.Scanner;
/**
* 题目描述
对于一个整数X,定义操作rev(X)为将X按数位翻转过来,并且去除掉前导0。例如:
如果 X = 123,则rev(X) = 321;
如果 X = 100,则rev(X) = 1.
现在给出整数x和y,要求rev(rev(x) + rev(y))为多少?
输入描述:
输入为一行,x、y(1 ≤ x、y ≤ 1000),以空格隔开。
输出描述:
输出rev(rev(x) + rev(y))的值
示例1
输入 123 100
输出 223
*/
public class 数字反转 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
Integer x = sc.nextInt();
Integer y = sc.nextInt();
System.out.println(rev(rev(x) + rev(y)));
}
}
private static int rev(int n) {
return n < 10 ? n : n < 100 ? n%10*10 + n/10 : n < 1000 ?
n%10*100 + n/10%10*10 + n/100 : n%10*1000 + n/10%10*100 + n/100%10*10 + n/1000;
}
}