杭电(hdu) 2091 PE 的问题

来源:互联网 发布:mysql中insert into 编辑:程序博客网 时间:2024/05/28 15:44

以下有两段代码,用两种不同的输入格式 会产生两种不同的文字理解”效果“

第一段:

#include<iostream>
#include<cstdio>
#include<iomanip>
using namespace std;
int main()
{
int length,Tlength1,Tlength2,flag=0;
char style;
scanf("%c",&style);
while(style!='@')
{
cin>>length;
if (flag==1) cout<<endl;
flag=1;
Tlength1=length;
Tlength2=length;
while(Tlength1-1)
{
cout<<setfill(' ')<<setw(Tlength1)<<style;
if (Tlength1-Tlength2) cout<<setw(Tlength2-Tlength1)<<style<<endl;
else cout<<endl;
Tlength1--;
Tlength2++;
}
cout<<setw(2*length-1)<<setfill(style)<<style<<endl;
cin>>style;
}


return 0;
}



第二段:

#include<iostream>
#include<cstdio>
#include<iomanip>
using namespace std;
int main()
{
int length,Tlength1,Tlength2;
char style;
scanf("%c",&style);
while(style!='@')
{
cin>>length;
Tlength1=length;
Tlength2=length;
while(Tlength1-1)
{
cout<<setfill(' ')<<setw(Tlength1)<<style;
if (Tlength1-Tlength2) cout<<setw(Tlength2-Tlength1)<<style<<endl;
else cout<<endl;
Tlength1--;
Tlength2++;
}
cout<<setw(2*length-1)<<setfill(style)<<style<<endl<<endl;
cin>>style;
}


return 0;
}


1、格式错误是第二段代码TUT

2、第一段代码的第一个例子之前不应该有空行,因此引入flag

3、setfill和setw一起用,cout<<setw(n)<<'A';指的是有N个字符,最后一个是A,setfill会一直影响后面。

0 0
原创粉丝点击