The subject asks you to write a program to print the given symbol into an hourglass shape. For example, given 17 "*", require printing in the following format
***** *** * ********
The so-called "hourglass shape" refers to the output of an odd number of symbols per line, the line symbol center alignment, the adjacent two lines sign number difference 2, the number of symbols from large to small order descending to 1, and then from small to large order increment;
Given any n symbols, it is not always possible to form an hourglass exactly. Require that the printed hourglass be able to use as many symbols as possible.
Input format:
The input gives 1 positive integers n (<=1000) and a symbol in one line, separated by a space in the middle.
Output format:
The largest hourglass shape consisting of a given symbol is printed first, and the number of symbols left unused in a row is finally output.
Input Sample:
19 *
Sample output:
* ********2
The number of layers to print is calculated first, and the corresponding print function is written for each line.
#include <bits/stdc++.h>using namespacestd;Charch;intnum;voidPrintintBlanksintstars);intMain () {CIN>> Num >>ch; intCNT =0; intLevel =0; while(1){ if(Level = =0) { level++; CNT++; } Else{ level++; CNT+=4*level-2; } if(CNT +4* (level+1)-2>num) { Break; } } intCurlevel =Level ; while(Curlevel >0) {print ( level-Curlevel, Curlevel); Curlevel--; } curlevel=2; while(Curlevel <=Level ) {Print ( level-Curlevel, Curlevel); Curlevel++; } printf ("%d\n", num-CNT); return 0;}voidPrintintBlanksintstars) { for(inti =0; I < blanks; ++i) {cout<<" "; } for(inti =0; I <2*stars-1; ++i) {cout<<ch; } cout<<Endl;}
Capouis ' CODE
pat-basic-1027-Print Hourglass