Title description
Time is passing by one minute and one second, let's print a simple hourglass!
Given any N symbols, it does not necessarily constitute an hourglass. It is required that the printed hourglass can use as many symbols as possible.
Remember to record the number of unused symbols.
enter
Enter a positive integer N (≤1000) and a symbol in one line, separated by spaces.
Output
First print out the largest hourglass shape composed of the given symbols, and finally output the number of unused symbols in one line.
Sample input
19 *
Sample output
*****
***
*
***
*****
2
Code content
#include <iostream>
using namespace std;
int main()
{
int i, n, sum = 0, m, sheng, j, r;
char c;
cin >> n >> c;
for (i = 1; sum <= n; i++)//计算共打印几行,还剩但是字符
{
if (i == 1)
sum = 1;
else
sum += 2 * (2 * i - 1);
j = i;
}
sum -= 2 * (2 * j - 1);
sheng =n-sum;
j = j - 1; //共j行
m = j;
for (i = 0; i < j-1; i++)//打印前j-1行
{
for(r=0;r<i;r++)//打印空格
cout << " ";
for(r=0;r<(2*m-1);r++)//打印字符
cout << c;
m--;
cout << endl;
}
m = j;
for (i = 1; i <= m; i++)//打印后j行
{
for(r=0;r<(j-i);r++)
cout << " ";
for(r=0;r<(2*i-1);r++)
cout << c;
cout << endl;
}
cout << sheng << endl;
return 0;
}