Section 3.3 of algorithm notes - Introduction simulation - > Graphic Output

A output ladder

Title Description

h is the height of the upper input side of the ladder, and h is the height of the upper input side.

input

An integer H (1 < = h < = 1000).

output

Trapezoid corresponding to h.

sample input

5

sample output

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

Code submission

#include<cstdio>
int main(){
	int h;
	while((scanf("%d",&h)!=EOF)){
		int blank=2*(h-1),n=h;
		for(int i=0;i<n;++i){
			for(int j=0;j<blank;++j) printf(" ");
			blank-=2;
			for(int j=0;j<h;++j) printf("*");
			h+=2;
			printf("\n");
		} 
	}
	return 0;
} 

Code ideas

Previously, only the increment of the * sign was considered to lead to format errors. Since the output of the graph is output to the right, it is also necessary to calculate the increment of the number of spaces on the left.

B Hello World for U

Title Description

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

h d

e l

l r

lowo

That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible – that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

input

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

output

For each test case, print the input string in the shape of U as specified in the description.

sample input

helloworld!

sample output

h !
e d
l l
lowor

Tips

The problem to be solved is to write a string in U shape. The first image to get this question is the writing method of U (not as much as the "fennel" of fennel beans). First write the first character in the first row, then the first character in the second row... Then the last row, and then the penultimate row... But in C language, if we want to write a U-shaped string in this way, we need to operate in the array. If it is direct output, it can only be output line by line from top to bottom. First, the first line, write the first character and the last character, the second line, write the second character and the penultimate character... The last line. It should be noted that except for the last line, all characters are output, and only two characters are output in each previous line. There are spaces in the middle to separate two characters of each line (the specific number of spaces will be calculated later).

With ideas, let's see the specific requirements. The length of the string is n, n1 and n3, representing the number of characters in each column on both sides. n2 represents the number of characters in the last line. A formula is given in the title:

n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

Carefully study this formula, where k is not greater than n2, that is, n1 and n3 are not greater than n2 and meet the maximum value of n1+n2+n3=N+2. Then naturally, n1=n3=(N+2)/3, n2=N+2-(n1+n3). That is, set side as the number of characters on both sides (including both ends of the last line), then side=n1=n3=(N+2)/3. Set mid as the number of characters left after removing two characters at both ends of the last line, mid=N-side*2 (total length minus the number of characters on both sides). At the same time, mid is also the number of spaces we need to leave in all the previous lines except the last line.

Finally, how to output the first character and the last character on the first line? Naturally, str[0] and str[len-1-i] (len is the length of the string, that is, N).

So the problem is solved perfectly. The steps are as follows:

1) Calculate the string length len;

2) Calculate the number of characters on both sides, side=(len+2)/3;

3) Calculate the number of characters in the middle of the last line (the number of spaces in the middle of each preceding line);

4) Output the corresponding characters for each line.

Since the topic is not difficult and there is nothing to pay special attention to, I won't write the points of attention. See the reference code for details.

Code submission

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n1,n2,n3,i,j;
	char str[100],ans[40][40];
	gets(str);
	int N=strlen(str);
    n1=(N+2)/3;
    n3=n1;
    n2=N+2-n1-n3;
    for(i=1;i<=n1;i++)
    {
    	for(j=2;j<=n2;j++)
    	{
    		ans[i][j]=' ';
		}
	}
	int index=0;
	for(i=1;i<=n1;i++)
	{
		ans[i][1]=str[index++];
	}
	for(j=2;j<=n2;j++)
	{
		ans[n1][j]=str[index++];
	}
	for(i=n3-1;i>=1;i--)
	{
		ans[i][n2]=str[index++];
	}
	for(i=1;i<=n1;i++)
    {
    	for(j=1;j<=n2;j++)
    	{
    		printf("%c",ans[i][j]);
		}
		printf("\n");
	}

	return 0;
}

C isosceles trapezoid

Title Description

Please enter the height h, and enter an isosceles trapezoid with the height h and the length of the upper and bottom sides H (for example, h=4, the figure is as follows).

     ****

    ******

   ********

  **********

input

Enter the first line to represent the number of samples m, and the next M lines are an integer h, H does not exceed 10.

output

Isosceles trapezoid corresponding to m case output requirements.

sample input

1
4

sample output

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

Submit code

#include <stdio.h>
#include <string.h>

int main()
{
    int h;
    int num;
    scanf("%d",&num);

    for(int i = 0;i < num;i++){
        scanf("%d",&h);
        for(int j = 0;j < h;j++){
            for(int k = 0;k < h - j - 1;k++){
                printf(" ");
            }
            for(int k = 0;k < h + 2 * j;k++){
                printf("*");
            }
            for(int k = 0;k < h - j - 1;k++){
                printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}

D hourglass figure tri2str [1 * +]

Title Description

Problem: input n and output the asterisk triangle with positive and inverted n layers. There is a space between the asterisks in the top grid of the first line. See the example for the effect

Input sample:

3

Output example:

  * * *
   * * 
    *
   * * 
  * * *

Data scale

1<= n <=50

Submit code

#include <stdio.h>
#include <string.h>

int main()
{
    int num;
    scanf("%d",&num);
    int cnt = num;

    for(int i = 0;i < 2 * num - 1;i++){
        for(int k = cnt;k < num;k++){
            printf(" ");
        }
        for(int j = cnt;j > 0;j--){
            if(j == 1) printf("*\n");
            else{
                printf("* ");   
            }
        }
        if(i < num - 1){
            cnt--;
        }else{
            cnt++;
        } 
    }


    return 0;
}

Keywords: C Algorithm linked list

Added by private_guy on Fri, 04 Feb 2022 16:55:45 +0200