首页 > 试题广场 >

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

[问答题]

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

#include <iostream>
using namespace std;
void print( int w )
{
for( int i = 1; i <= w; i ++ )
{
for( int j = 1; j <= i; j ++ )
cout << i << " ";
cout << endl;
}
}
int main()
{
print( 5 );
}

程序运行结果:

1

2  2

3  3  3

4  4  4  4

5  5  5  5  5

推荐
#include<iostream>
using namespace std;
void print(int w)
{
 int i;
  if( w )
    { print( w-1 );
      for( i=1; i<=w; i++ )
cout << w << "  ";
      cout << endl;
     }
}
void main()
{
  print( 5 );
}

发表于 2018-05-07 11:38:41 回复(0)