输入N位数,比如当N为3时,打印出从0到999
#include <iostream>
using namespace std;
void _print(char *buff, const int size, int index = 0)
{
if (index == size) {
cout << buff << endl;
return;
}
for (int i = 0; i < 10; i++)
{
buff[index] = i + '0';
_print(buff, size, index + 1);
}
}
void print(char *buff, const int size)
{
if (buff == NULL || size <= 0)
return;
buff[size - 1] = 0;
_print(buff, size - 1);
}
void main()
{
char buff[4];
print(buff, 4);
}
本文介绍了一个使用C++实现的递归算法,该算法能够打印出从0开始的所有N位数,例如当N为3时,将打印从0到999的所有整数。通过递归函数的调用实现了数字的逐位生成,并使用了简单的for循环来完成数字的打印。
360

被折叠的 条评论
为什么被折叠?



