1036 跟奥巴马一起编程 (15分)

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

输入格式:

输入在一行中给出正方形边长 N(3)和组成正方形边的某种字符 C,间隔一个空格。

输出格式:

输出由给定字符 C 画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的 50%(四舍五入取整)。

输入样例:

10 a
 

输出样例:

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa


 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 using namespace std;
 5 
 6 int main(){
 7     int n=0;
 8     int m=0;
 9     char c;
10     cin>>m>>c;
11     if(m%2==0){
12         n=m;
13     }else{
14         n=m+1; 
15     }
16     for(int i=0;i<n/2;i++){        //横行数 
17         if(i==0 || i== n/2-1){    //第一行和最后一行全部输出字母 
18             for(int j=0;j<m;j++){
19                 cout<<c;
20             }
21             cout<<"\n";
22         }else{        //中间行输出字母加空格 
23             for(int j=0;j<m;j++){
24                 if(j==0 || j==m-1){
25                     cout<<c;
26                 }else{
27                     cout<<" ";
28                 }
29             }
30             cout<<"\n";            
31         }
32         
33         
34     }
35     
36     return 0;
37 } 

 

posted @ 2020-02-06 16:36  葛杨杨  阅读(191)  评论(0)    收藏  举报