P1307题目要求
反转的话,就不能考虑number了,应该当做char[]/String处理,末尾的0是要除掉的,负号还应该保留并不能进入number倒置的过程里,所以可以用flag(boolean)来标定。
当然,其实倒序输出就OK~~
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Long num = scanner.nextLong();
scanner.close();
boolean neg = false;
if (num < 0) {
System.out.print("-");
neg = true;
}
char[] chars = num.toString().toCharArray();
int counter = 0;
for (int i = chars.length-1; i >= 0; i--) {
if (chars[i] != 48) {
break;
}
counter++;
}
if (neg) {
for (int i = chars.length-counter-1; i >= 1; i--) {
System.out.print(chars[i]);
}
} else {
for (int i = chars.length-counter-1; i >= 0; i--) {
System.out.print(chars[i]);
}
}
}
}