-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintToN12.java
More file actions
65 lines (61 loc) · 1.7 KB
/
PrintToN12.java
File metadata and controls
65 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package offer;
import java.util.*;
/*
* 打印1到最大的n位数
*/
public class PrintToN12 {
public void printN(int n){
if(n <= 0){
return;
}
char[] number = new char[n];
for (int i = 0; i < number.length; i++) {
number[i] = '0';
}
while(!Increment(number)){
PrintNumber(number);
}
}
public void PrintNumber(char[] c){
String str = new String(c);
int i = 0;
//开始的0不打印
for (i = 0 ; i < str.length() ; i++) {
if(str.charAt(i) - '0' != 0) {
break;
}
}
System.out.println(str.substring(i, str.length()));
}
public boolean Increment(char[] c){
boolean isOverflow = false;
//进位
int nTakeOver = 0;
int nsum = 0;
//从低位开始
for (int i = c.length - 1; i >= 0; i--) {
//得到当前位的值是多少
nsum = c[i] - '0' + nTakeOver;
//然后最低位加1,其他高位只能是加上进位
if(i == c.length - 1) nsum++;
if(nsum >= 10){
if(i == 0){
//溢出了,最高位等于9时,加1后
isOverflow = true;
}else{
c[i] = '0';
nTakeOver = 1;
}
}else{
c[i] = (char)(nsum + '0');
break;
}
}
return isOverflow;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
new PrintToN12().printN(n);
}
}