C++题目,高手请进.

2024-12-27 19:55:55
推荐回答(1个)
回答1:

#include
#include
using namespace std;

void f1( int n )
{
for ( int i=-n; i<=n; ++i ) {
for ( int j=-n; j<=n; ++j ) {
cout << ((abs(i)>=abs(j)) ? abs(i) : abs(j) );
}
cout << endl;
}
cout << endl;
}

void f2( int n )
{
for ( int i=2*n; i>=0; --i ) {
for ( int j=0; j<=2*(n-abs(i-n)); ++j ) {
cout << (i+1)/2;
}
cout << endl;
}
cout << endl;
}

void f3( int n )
{
for ( int i=-n; i<=n; ++i ) {
cout << setw(abs(i)+1);
for ( int j=-(n-abs(i)); j<=(n-abs(i)); ++j ) {
cout << abs(i)+abs(j);
}
cout << endl;
}
cout << endl;
}

int main()
{
f1( 3 );
f2( 4 );
f3( 4 );

return 0;
}