把下列程序中的print()函数改写为等价的递归函数

来源:百度知道 编辑:UC知道 时间:2024/04/29 10:27:53
#include <iostream.h> void print(int w){for(int i=1;i<=w;i++){for(int j=1;j<=i;j++)cout<<i<<" ";cout<}}void main(){print(5);}

原程序这种书写风格实在是糟糕透顶,建议不要再写成这样!

递归函数:

void print(int w)
{
static n=1,i=1;
if (n<=w) {
if (i++<=n) {
cout<<n<<' ';
print(w);
}
i=1;
n++;
print(w);
}

}

--------------------------------------------------
完整示例程序:

#include <iostream.h>
using namespace std;
void print(int w)
{
static n=1,i=1;
if (n<=w) {
if (i++<=n) {
cout<<n<<' ';
print(w);
}
i=1;
n++;
print(w);
}

}
void main()
{
print(6);
}

以前做过 现在忘拉 =我回家找找