#include "iostream"
using namespace std;
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 print1ToMaxOfNDigitsRecursively(char *number,int length,int index)
{
if (index==length-1)
{
printNumber(number);
return;
}
for (int i = 0; i < 10;i++)
{
number[index + 1] = '0' + i;
print1ToMaxOfNDigitsRecursively(number, length, index +1);
}
}
void print1ToMaxOfNDigits(int n)
{
if (n <= 0)
return;
char *number = new char[n + 1];
number[n] = '\0';
for (int i = 0; i < 10;i++)
{
number[0] = i + '0';
print1ToMaxOfNDigitsRecursively(number, n, 0);
}
delete []number;
}
int main()
{
print1ToMaxOfNDigits(2);
system("pause");
return 0;
}
剑指offer面试题12:打印1到最大的n位数(2)
最新推荐文章于 2024-05-05 09:47:28 发布