递归
#include<iostream>
using namespace std;
void PrintfNumber(char num[])
{
bool isBegin0 = true;
int nlength = strlen(num);
for (int i = 0; i < nlength; ++i)
{
if (isBegin0&&num[i] != '0')
{
isBegin0 = false;
}
if (!isBegin0)
{
cout << num[i];
}
}
}
void PrintToMaxDigits2(char *num, int length, int index)
{
if (index == length - 1)
{
PrintfNumber(num);
return;
}
for (int i = 0; i < 10; i++)
{
num[index + 1] = i + '0';
PrintToMaxDigits2(num, length, index + 1);//
}
}
void PrintToMaxofDigits(int n)
{
if (n <= 0)
{
return;
}
char *num = new char[n + 1];
num[n] = '\0';
for (int i = 0; i < 10; i++)
{
num[0] = i + '0';
PrintToMaxDigits2(num, n, 0);
}
}
int main()
{
int n;
cin >> n;
PrintToMaxofDigits(n);
system("pause");
}
详解
//以n等于3为例
void PrintToMaxDigits2(char *num, int length, int index)
{
if (index == length - 1)
{
PrintfNumber(num);
return;
}
for (int i = 0; i < 10; i++)
{
num[index + 1] = i + '0';
PrintToMaxDigits2(num, length, index + 1);//
/*
index=1;
for (int i = 0; i < 10; i++)
{
num[index + 1] = i + '0';
PrintToMaxDigits2(num, 3, index + 1);
if (index == length - 1)
{
//打印了1-9,这时num[1]还是1,当num[1]是2时再打印1-9
PrintfNumber(num);
return;
}
}
*/
}
}