#include "iostream"
using namespace std;
bool Increment(char *number)
{
int length = strlen(number);
int index = length-1;
bool isOverFlow = false;
for (int i = index; i >= 0;i--)
{
if (isOverFlow&&number[i] != '9')
{
number[i] = (int)number[i] + 1;
break;
}
if (number[i]=='9')
{
if (i==0)
return false;
isOverFlow = true;
number[i] = '0';
}
else
{
number[i] = (int)number[i] + 1;
break;
}
}
return true;
}
void printNumber(char *number)
{
int length = strlen(number);
bool isBegin0 = true;
for (int i = 0; i < length;i++)
{
if (isBegin0&&number[i] != '0')
isBegin0 = false;
if (!isBegin0)
cout << number[i];
}
cout << endl;
}
void print1ToMaxOfNDigits(int n)
{
if (n < 0)
return;
char *number = new char[n+1];
memset(number, '0', n);
number[n] = '\0';
while (Increment(number))
{
printNumber(number);
}
delete []number;
}
int main()
{
print1ToMaxOfNDigits(2);
system("pause");
return 0;
}
剑指offer面试题12:打印1到最大的n位数(1)
最新推荐文章于 2022-03-14 21:10:26 发布