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 }

 

 

转载于:https://www.cnblogs.com/I-am-Betty/archive/2011/12/09/2282511.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值