00-Self-Test 1. Print the hourglass (20), 00-hourglass
This question requires you to write a program to print the given symbol into the shape of an hourglass. For example, if 17 "*" characters are specified, print them in the following format:
***** *** * ********
The so-called "hourglass shape" refers to the odd number of characters output for each row; the center of each line is aligned; the number difference between two adjacent lines is 2; the number of symbols first decreases from large to small to 1, the number of first and last symbols is equal.
Given any N symbols, an hourglass may not be formed. The printed hourglass is required to use as many symbols as possible.
Input Format:
Enter a positive integer N (<= 1000) and a symbol in a row, separated by spaces.
Output Format:
Print the largest hourglass shape composed of given symbols, and output the remaining unused symbols in a row.
Input example:
19 *
Output example:
***** *** * ********2
#include<iostream>#include<cmath>using namespace std;int main(){int N;char C;cin>>N>>C;int k = sqrt((N+1)/2);for(int i=1;i<=k;i++){for(int j =1;j<i;j++){cout<<' ';} for (int j = 2*(k - i) + 1; j >= 1; j--) {cout<<C;}cout<<endl;}for(int i=1;i<=k-1;i++){for(int j=1;j<=k-i-1;j++){cout<<' ';}for(int j=1;j<=2*i+1;j++){cout<<C;}cout<<endl;}cout<<N-2*k*k+1<<endl; return 0;}