『C/C++』Eg5: Print hourglass

This question requires you to write a program to print the given symbol into the shape of an hourglass. For example, given 17 "*", it is required to print in the following format

*****
 ***
  *
 ***
*****

The so-called "hourglass shape" means that each line outputs an odd number of symbols; the centers of symbols in each line are aligned; the number of symbols in two adjacent lines differs by 2; the number of symbols first decreases from large to small to 1, and then increases in order from small to large; The numbers are equal.

Given any number of N symbols, it is not necessarily possible to form exactly an hourglass. It is required that the printed hourglass can use up as many symbols as possible.

Input format:

The input gives 1 positive integer N (≤1000) and a symbol on one line, separated by spaces.

Output format:

First print the largest hourglass shape made of the given symbols, and finally output the number of unused symbols left on one line.

Input sample:

19 *

Sample output:

*****
 ***
  *
 ***
*****
2

answer:

Presumably, when many friends first saw this question, they thought it was very simple and easy to get started, but when they really thought about it, they found that it was still a bit difficult to start. So I will now explain my thinking and solution to this question~

Breakdown of the first part

First of all, this "hourglass" is symmetrical up and down. We can first find how many layers are above the middle (including the middle layer), and after finding the number of upper layers, subtract one, which is the number of lower layers.
We use the total number subtraction method to find the number of layers above:
insert image description here
except for the first time (that is, subtracting the middle) which is one, the others are all subtracted by 2*i, and counting is required for each subtraction. Keep decreasing until x is a negative number, and the counting stops.
For example: 14 *
where x is 14, the first time x=14-1=13; count=1; the
second time x=13-2x3=7; count=2;
the third time x=7-2x5=-3 <0
jumps out of the loop, because the count is 2, so there are two layers above.
Since subtracting the first layer and subtracting multiple layers is twice as small, write one layer and multiple layers separately.
From this idea, the code is as follows:

int Count(int x)
{
    
    
    int flag = 1, count = 1, i = 3;
    if (x < 7)//一层
        return 1;
    else//多层
    {
    
    
        x -= 1;//先减去中间层
        while (flag)
        {
    
    
            x -= 2 * i;
            if (x >= 0)
            {
    
    
                flag = 1;
                count++;
            }
            else
                flag = 0;
            i += 2;
        }
    }
    return count;
}

Here, the upper layer count is calculated, and the lower layer number is count-1;


Part Two: Printing

upper half

After finding the number of layers here, start thinking about printing.
Printing is divided into spaces and *. Here I use tables to show the connections between the various parts.
Since you only need to print the space before each layer *,
![Insert picture description here](https://img-blog.csdnimg.cn/11efb1b893864fd6b35df406f852a38f.png

The form is as follows:

for(j=0;j<count;j++) number of spaces
j=0 0
j=1 1
j=2 2
d=n n

So the code to print spaces is as follows:

for (i = 0; i < count; i++)
{
    
    
    for (j = 0; j < i; j++)
        printf(" ");
}

Now let's consider the part of printing *, still the same, the form is as follows:

for(j=0;j<count;j++) the number of *
j=0 2*count-1
j=1 2*(count-1)-1
j=2 2*(count-2)-1
d=n 2*(count-j)-1

So the code is as follows:

int k = count;
for (i = 0; i < count; i++)
{
    
    
    for (j = 0; j < 2 * k - 1; j++)
    {
    
    
        printf("%c", c);
    }
    k--;
}

Ok, so far, the first half of the hourglass is complete~


lower half

As usual, let’s look at the space part first, as shown in the figure below,
insert image description here
here we can use mathematical formulas to find the number of spaces in the first layer, and the spaces in each layer below only need to be based on the number of spaces in the previous layer -1. Can.
Find the number of spaces in the first layer:
the number of spaces in the first layer = (use the number of '*' in the last layer - the number of '*' in the first layer)/2
how to find the number of '*' in the last layer, use just The above formula: 2*count-1,
so the code to print spaces is as follows:

int l=0;
l = count * 2 - 1;
l = (l - 3) / 2;
for (i = 0; i < count - 1; i++)
{
    
    
    for (j = 0; j < l; j++)
        printf(" ");
    l -= 1;
}

Ok, after printing the number of spaces in the lower half, let's print the * in the lower half. It is very simple here, since the number of * in the first layer is 3, which remains unchanged, the next layer only needs to add 2 to the original basis.
So the code to print '*' is as follows:

int n = 3;
for (i = 0; i < count - 1; i++)
{
    
    
    for (j = 0; j < n; j++)
    {
    
    
        printf("%c", c);
    }
    n += 2;
}

At this point, the printing is complete, and now it is just a matter of outputting the number of unused symbols~


output the number of unused symbols

You only need to count each print* in the print function.

int Printc(int count, char c)
{
    
    
    int count1 = 0;
    for (i = 0; i < count; i++)
    {
    
    
        for (j = 0; j < 2 * k - 1; j++)
        {
    
    
            printf("%c", c);
            count1++;
        }

    }
    for (i = 0; i < count - 1; i++)
    {
    
    

        for (j = 0; j < n; j++)
        {
    
    
            printf("%c", c);
            count1++;
        }
    }
}

The total code is as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int Count(int x)
{
    
    
    int flag = 1, count = 1, i = 3;
    if (x < 7)
        return 1;
    else
    {
    
    
        x -= 1;
        while (flag)
        {
    
    
            x -= 2 * i;
            if (x >= 0)
            {
    
    
                flag = 1;
                count++;
            }
            else
                flag = 0;
            i += 2;
        }
    }
    return count;
}

int Printc(int count,char c)
{
    
    
    int i = 0, j = 0, k = 0, l = 0, n = 3;
    int count1 = 0;
    l = count * 2 - 1;
    l = (l - 3) / 2;
    k = count;
    for (i = 0; i < count; i++)
    {
    
    
        for (j = 0; j < i; j++)
            printf(" ");
        for (j = 0; j < 2 * k - 1; j++)
        {
    
    
            printf("%c", c);
            count1++;
        }
        printf("\n");
        k--;
    }
    for (i = 0; i < count - 1; i++)
    {
    
    
        for (j = 0; j < l; j++)
            printf(" ");
        for (j = 0; j < n; j++)
        {
    
    
            printf("%c", c);
            count1++;
        }
        printf("\n");
        l -= 1;
        n += 2;
    }
    return count1;
}

int main()
{
    
    
    int x = 0, y = 0;
    char c = 'a';
    scanf("%d %c", &x, &c);
    y = Count(x);
    int F=Printc(y,c);
    printf("%d\n", x-F);
    return 0;
}



Alright~ This is the end of today's C language sharing questions, see you next time

Guess you like

Origin blog.csdn.net/hsjsiwkwm/article/details/130948067