Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/littlehaes/article/details/91411200
welcome to my blog
Offer surface prove safety questions 17 (java version): Printing from 1 to the maximum number of bits n
Questions asked
Enter the number n, in order to print out from a maximum of n-bit decimal number, such as three inputs, print the 1,2,3, ..., 999
Thinking
- The biggest difficulty of this problem is that when n takes very large, int, long time not enough, starting a string representation (the problem of large numbers)
- Details of a string of numbers plus one o'clock to consider more specific see Note
- How to find out when printing the first non-zero digit is also very clever, setting the flag flag, ture current is 0, the first non-zero value until it hits, flag becomes false, which means you can start printing
- Also clear: jinWeiFlag determined by the value of the current bit, jinWeiFlag effect on the next bit
the complexity
public class MST17 {
public static void main(String[] args) {
int n = 3;
Print1ToMaxOfNDigits(n);
}
public static void Print1ToMaxOfNDigits(int n){
// 健壮性判断
if(n <= 0)
throw new RuntimeException("n最小值为1");
// 正式执行
/*
本题最需要注意的就是n过大时,long类型也不好使了,此时应该用char[]表示数字(大数问题), 打印string类型的数字
关键在于如何用string表示数字? 如何知道已经打印到最大的数字? 毕竟不能作比较
*/
char[] number = new char[n+1]; //number[0]作为最大数字的进位标志位
for(int i=0; i<n+1; i++)
number[i]='0';
while(!increment(number)){ // 没到最大值时,就不断循环打印
PrintNumber(number);
}
}
private static boolean increment(char[] number){
// 该函数执行数字加1的操作
// 当前位的进位标志,也就是当前位是否有进位
int jinWeiFlag = 0;
// 一位一位的处理; 从个位开始
for(int i=number.length-1; i>0; i--){
// nSum表示当前位是几
// 除个位外的位, 其值等于原来的值(number[i] - '0')加上jinWeiFlag
// 个位的值等于原来的值加1
int nSum = number[i] - '0' + jinWeiFlag;
if(i == number.length-1)
nSum++;
// 接着判断当前位改变后是否能进位
// 大于等于10则进位;否则不变
// 有进位的时候要注意是谁有进位! 最高位有进位的话表示改变前已经达到最大值;
if(nSum>=10){
if(i==1) // 如果最高位有进位
number[0]='1'; // 这个值用于终止循环
else{ // 如果非最高位有进位
jinWeiFlag=1;
number[i] = (char)(nSum-10+48); // +48是为了转换成char类型的数字
}
}
else{
// jinWeiFlag由当前位值的决定,影响的是下一位取值
jinWeiFlag = 0; // 清除进位状态, 下面将会根据改变后的值重新判断jinWerFlag的取值
number[i] = (char)(nSum+48);
}
}
return number[0] == '1';
}
private static void PrintNumber(char[] number){
//从第一个非零数字开始打印!
boolean flag=true; // 当前位还是0则为ture
for(int i=1; i<number.length; i++){
if(flag && number[i] != '0')
flag = false;
if(!flag && i==number.length-1){
System.out.println(number[i]);
break;
}
if(!flag)
System.out.print(number[i]);
}
}
}
Better code
Thinking
- Print from 1 to n 9, n locations each location has 10 kinds of selection, 10 n values (not including the word is 0 10 n - 1 values)
- The above process can be implemented recursively, recursion termination condition is: the currently processed is the last one
- 0-9 java will convert to the char, using (char) (i + 48), do not directly use (char) i, i will so convenient printing into Unicode? Continue compaction process following the brush in question basis
public class MST17 {
public static void main(String[] args) {
int n = 4;
Print1ToMaxOfNDigits2(n);
}
public static void Print1ToMaxOfNDigits2(int n){
// 健壮性判断
if(n<1)
throw new RuntimeException("n最小值为1");
// 正式执行
char[] number = new char[n];
for(int i=0; i<number.length; i++)
number[i]='0';
for(int i=0; i<10; i++){
number[0] = (char)(i+48);
Print1ToMaxOfNDigitsRecursive(number, 0, i);
}
}
public static void Print1ToMaxOfNDigitsRecursive(char[] number, int curr, int value){
// 递归终止条件:当前位是个位
if(curr == number.length-1)
PrintNumber2(number);
else{
// 继续处理处理下一位
for(int i=0; i<10; i++){
number[curr+1] = (char) (i+48);
Print1ToMaxOfNDigitsRecursive(number, curr+1, i);
}
}
}
private static void PrintNumber2(char[] number){
//从第一个非零数字开始打印!
boolean flag=true; // 当前位还是0则为ture
for(int i=0; i<number.length; i++){
if(flag && number[i] != '0')
flag = false;
if(!flag && i==number.length-1){
System.out.println(number[i]);
break;
}
if(!flag)
System.out.print(number[i]);
}
}
}