标签:cout name filter 代码 眼睛 int through std nbsp
突然发现之前有一些题没有写博客,在这里补上吧。
1.题目很好理解,但是代码死活打不出来。(眼睛:真简单;脑子:放pi,你想多了)想了想觉得还挺好做,但真正上手的时候依然死去活来。
2.分析题目
根据题目的要求,直接想到循环(这可一定)
实心的矩形比较好实现,循环套循环,两轮操作,每次操作输出一个字符就行。
直接分析空心的。
现在有两种方案:
(1):从全局考虑,直接两个循环;
(2):两个循环,把第一行和最后一行(字符是满的)和其他行(只有首尾有字符)分隔开来。
分别按不同的情况输出。
3.肯定会选第二种,因为第二种代码实现的时候比较简单(当我没说)
下面是完整的代码:
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b; 6 char ch; 7 bool x; 8 int i,j; 9 10 cin>>a>>b; 11 cin>>ch; 12 cin>>x; 13 14 if(x) 15 { 16 for(i=1; i<=a; i++) 17 { 18 for(j=1; j<=b; j++) cout<<ch; 19 cout<<endl; 20 } 21 } 22 else 23 { 24 for(i=1; i<=a; i++) 25 { 26 for(j=1; j<=b; j++) 27 { 28 if((i==1)||(i==a)||(j==1)||(j==b)) 29 cout<<ch; 30 else 31 cout<<" "; 32 } 33 cout<<endl; 34 } 35 } 36 cout<<endl; 37 return 0; 38 }
根据代码实现结果与样例相同。结束。
标签:cout name filter 代码 眼睛 int through std nbsp
原文地址:https://www.cnblogs.com/Kyriech-Francis/p/12257393.html