정수 역순 출력 몇 가지 방법 소결 (자바 구현)
1980 단어 알고리즘 경기 관련
정수를 역순으로 한 수조에 넣기 (귀속 실현 요구)
구현 방법:
1. 반복(잘못된 문자 포함)
2, 문자 작업(잘못된 문자 배열 포함)
3. 문자 조작(잘못된 문자가 없는 배열)
Java 구현 소스 코드(메서드 1):
public class zhengshu {
// ( )
public void reverse(StringBuffer result, int num) {
if (num != 0) {
result.append(num % 10);
reverse(result, num / 10);
}
}
public static void main(String[] args) {
StringBuffer result = new StringBuffer();
new zhengshu().reverse(result, 1024);
System.out.println(result);
}
}
PS: 배열에 잘못된 문자가 있습니다(예: 0).
Java 구현 소스 코드(메서드 2):
import java.util.Scanner;
public class tter {
//
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
int[] resu = new int[str.length()];
int temp, i = 0;
int res = Integer.parseInt(str);
for (i = 0; i < str.length(); i++) {
temp = res % 10;
resu[i] = temp;
res = res / 10;
}
for (i = 0; i < str.length(); i++) {
if(resu[i]==0){
continue;
}
System.out.print(resu[i]);
}
}
}
PS: 배열에 잘못된 문자가 저장됩니다(예: 0).
Java 구현 소스 코드(메서드 3):
import java.util.Scanner;
public class zhengshu {
//
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String strOld = in.next();
int[] resu = new int[strOld.length()];
int temp, i = 0, j = 0;
for (j = strOld.length() - 1; j >= 0; j--) { // 0 , 0
if (!"0".equals(strOld.substring(j, j + 1)))
break;
}
String strNew = strOld.substring(0, j + 1);
int res = Integer.parseInt(strNew);
for (i = 0; i < strNew.length(); i++) {
temp = res % 10;
resu[i] = temp;
res = res / 10;
}
for (i = 0; i < strNew.length(); i++) {
if(resu[i]==0){
continue;
}
System.out.print(resu[i]);
}
}
}
PS: 잘못된 문자가 배열에 저장되지 않았습니다(예: 0).
독자들은 삼자의 차이를 주의해서 체득하기를 바란다.