打印1到最大的n位数,例如n = 2时打印出1,2。。。99
这里用了分治(应该是分治吧。。。),直接贴代码,挺有意思的:
#include <iostream>
#include <string>
using namespace std;
void Print(string s)
{
for (int i = 0; i != s.size(); ++i)
{
if (s[i] != '0')
{
cout << s.substr(i) << " ";
break;
}
}
}
void PrintHelp(char *s, int n, int start)
{
if (start <= n - 1)
{
for (int i = 0; i != 10; ++i)
{
s[start] = i + '0';
if (start < n -1)PrintHelp(s, n, start + 1);
else Print(s);
}
}
}
void PrintStr(int n)
{
if (n <= 0)
{
cout << "error" << endl;
return;
}
else
{
char *s = new char[n+1];
s[n] = '\0';
PrintHelp(s, n, 0);
}
}
void main()
{
PrintStr(2);
system("pause");
}