单循环打印向中心递增的矩形

1 view
Skip to first unread message

bruc...@gmail.com

unread,
Dec 20, 2007, 1:11:58 AM12/20/07
to 星星爱CPP
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;

void foo( int n )
{
const int LEN = 10;
const char* L = "0123456789";
const char* R = "9876543210";
assert( n>=0 && n<=LEN*2 );

for( int i=0; i<n; ++i )
{
int c = (n-abs(i*2+1-n))/2;

cout.write( L, c )
<< setw(n-2*c) << setfill(L[c]) << ""
<< (R+LEN-c)
<< endl;
}
}

int main( void )
{
foo( 15 );

return 0;
}
Reply all
Reply to author
Forward
0 new messages