PAT B: 1036 Program with Obama (15 points)

Please write a program to find the English letter that appears most frequently in a given text.

Input format:

The input gives the side length N of the square (3≤N≤20) and some characters C forming the sides of the square in one line, separated by a space.

Output format:

Output the square drawn by the given character C. But notice that the row spacing is larger than the column spacing, so in order to make the result look more square, the number of rows we output is actually 50% of the number of columns (rounded up).

Input sample:

10 a
Sample output:

aaaaaaaaaa
a                  a
a                  a
a                  a
aaaaaaaaaa

Idea: The
first line and the last line are output directly, and the number of lines in the middle uses a double loop.
Rounding function: round()

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
	
	int main()
	{
    
    
	int row=0,list=0;
	char c;
	cin>>list>>c;
	row=round(list*0.5); 				 //行数是列数的50%,四舍五入 
	
	for(int i=1;i<=list;i++)
	{
    
    
		printf("%c",c);
	}
	  cout<<endl;  //第一行输出以后就换行
	   
	    //中间的行
	for(int i=1;i<=row-2;i++) //控制行数,去除首尾,那么剩下row-2行 
	 {
    
     
		  for(int j=1;j<=list;j++)  //list列 
		  {
    
    
		    if(j==1) printf("%c",c);
		    else if(j==list) printf("%c\n",c);
		    else printf(" ");
		  } 
		
	 }
	 
	 //最后一行
	 	for(int i=1;i<=list;i++)
	{
    
    
		printf("%c",c);
	}
	 	  cout<<endl;  
	}

result:

Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/113814310