Print a rhombus (C++)

 

当我纠结于怎么输出n个空格的时候, 一个内置方法轻描淡写地就把问题给解决了:  cout<<setw(n)<<n<<endl; 打印n个空格(为了辨别,后面加了一个字母n)不过要加#include <iomanip>

下面做一个简单的练习:打印一个菱形框

实现方法如下:

View Code
 1 #include "stdafx.h"
2 #include <iomanip>
3 #include <iostream>
4 using namespace std;
5
6
7 int _tmain(int argc, _TCHAR* argv[])
8 {
9 int i,j,m,n;
10 for (i=0;i<=8;i++) //print the above half of the rhombus
11 {
12 m=9-i;
13 n=2*i+1;
14 cout <<setw(m)<< "/"<<setw(n)<<"\\"<<endl;
15 }
16 for(j=8;j>=0;j--) //print the below half of the rhombus
17 {
18 m=9-j;
19 n=2*j+1;
20 cout<<setw(m)<<"\\"<<setw(n)<<"/"<<endl;
21 }
22 return 0;
23 }

 

 

posted @ 2011-12-09 17:16  AnnieBy  阅读(336)  评论(0编辑  收藏  举报