PAT - B 1030 Programming with Obama

1036. Programming with Obama (15)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

US President Barack Obama not only called on everyone to learn to program, but even led by example to write code, becoming the first president in American history to write computer code. At the end of 2014, to celebrate the official launch of Computer Science Education Week, Obama wrote a very simple computer code: draw a square on the screen. Now you can draw with him too!

Input format:

Enter a square with side length N (3<=N<=20) and some kind of character C that makes up the sides of the square in one line, separated by one 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 to make the result look more square, we actually output the number of rows as 50% of the number of columns (rounded up).

Input sample:
10 a
Sample output:
aaaaaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaaaaa

#include<cstdio>

using namespace std;

int main(){
	int n;
	char ch;
	scanf("%d %c", &n, &ch);
	int x = n % 2 == 0 ? n / 2 : n / 2 + 1;
	for(int i = 0; i < x; i++){
		if(i == 0 || i == x-1)for(int i = 0; i < n; i++)printf("%c", ch);
		else {
			for(int i = 0; i < n; i++){
				if(i == 0 || i == n-1)printf("%c", ch);
		        else printf(" ");
			}
		}
		printf("\n");
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325754857&siteId=291194637