Graphics output - Algorithm Notes

US President Barack Obama not only appeal to everyone to learn programming, and even set an example to write code, write computer code to become the first president in American history. The end of 2014, to celebrate the "Computer Science Education Week" was officially launched, Obama wrote the computer code is very simple: draw a square on the screen. Now you draw it with him!

Input formats:

Input gives the square side length N (3≤N≤20) in a row and consisting of one of the character square edges C, a space interval.

Output formats:

C output from the given character drawn square. But noted that the line spacing is larger than the column spacing, so in order to make the results look more like a square, the number of lines we output is actually 50% of the number of columns (rounded to the nearest integer).

Sample input:

10 a

 

Sample output:

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa

Code:

#include<iostream>
#include<string>
using namespace std;

int main(){
	int n;
	char c;
	cin >> n >> c;
	for (int i = 0; i < n; i++){
		cout << c;
	}
	cout << endl;
	for (int i = 0; i < (n / 2+n%2)-2; i++){
		cout << c;
		for (int j = 0; j < n - 2; j++)
			cout << " ";
		cout << c;
		cout << endl;
	}
	for (int i = 0; i < n; i++){
		cout << c;
	}
	return 0;
}

 

 

Published 98 original articles · won praise 2 · Views 3710

Guess you like

Origin blog.csdn.net/qq_30377869/article/details/105000703