[C language] online OJ questions - introduction training for beginners of Niuke network programming BC53-BC70 graphics

Posted by pixeltrace on Mon, 27 Dec 2021 04:19:22 +0100

BC53 calculation of univariate quadratic equation

describe
Input the values of a, B and C from the keyboard, program, calculate and output the root of the univariate quadratic equation ax2 + bx + c = 0. When a = 0, output "not rectangular equation". When a ≠ 0, calculate and output the root of the equation according to the three cases of △ = b2 - 4ac.
Enter Description:
Multiple sets of inputs, one line, containing three floating-point numbers a, b, c, separated by a space, represent the coefficients of the univariate quadratic equation ax2 + bx + c = 0.
Output Description:
For each group of inputs, output a row and output the root of the univariate quadratic equation ax2 + bx +c = 0.

If a = 0, output "not rectangular equation";

If a ≠ 0, there are three cases:

△ = 0, then the two real roots are equal, and the output form is: x1=x2 =.

△ > 0, then the two real roots are unequal, and the output form is: x1 =; X2 =..., where x1 < = x2.

△ < 0, then there are two imaginary roots, then output: x1 = real part imaginary part i;x2 = real part + imaginary part i, that is, the imaginary part coefficient of x1 is less than or equal to the imaginary part coefficient of x2. When the real part is 0, it can not be omitted. Real part = - b / (2a), imaginary part = sqrt(- △) / (2a)

All real numbers shall be accurate to 2 digits after the decimal point, and there shall be no space between numbers and symbols.

Example 1
Input:
2.0 7.0 1.0
Output:
x1=-3.35;x2=-0.15

Example 2
Input:
0.0 3.0 3.0
Output:
Not quadratic equation

Example 3
Input:
1 2 1
Output:
x1=x2=-1.00

Example 4
Input:
2 2 5
Output:
x1=-0.50-1.50i;x2=-0.50+1.50i

Example 5
Input:
1 0 1
Output:
x1=0.00-1.00i;x2=0.00+1.00i

Title Link

#include <stdio.h>
#include <math.h>

int main()
{
    float a, b, c;
    while ((scanf("%f %f %f", &a, &b, &c)) != EOF)
    {
        float e = b * b - 4 * a * c;
        float s = sqrt(e);
        if (a == 0)
        {
            printf("Not quadratic equation\n");
        }
        if (a != 0)
        {
            if (e == 0)
            {
                printf("x1=x2=%.2f\n", (-b + s) / (2 * a));
            }
            if (e > 0)
            {
                printf("x1=%.2f;x2=%.2f\n", (-b - s) / (2 * a), (-b + s) / (2 * a));
            }
            if (e < 0)
            {
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi", -b/(2*a), sqrt(-e)/(2*a), -b/(2*a), sqrt(-e)/(2*a));
            }
        }

    }

    return 0;
}

BC54 get month days

describe
KiKi wants to get how many days there are in a certain month of a certain year. Please help him program it. Enter the year and month to calculate the number of days in this month of the year.
Enter Description:
Multiple sets of inputs, with two integers in one line, representing the year and month respectively, separated by spaces.
Output Description:
For each group of inputs, the output is a line and an integer, indicating the number of days in this month of the year.
Example 1
Input:
2008 2
Output:
29

Title Link

#include <stdio.h>

int is_leap_year(int year)
{
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		return 1;
	}
	else
		return 0;
}

int main()
{
	int year = 0;
	int month = 0;
	while ((scanf("%d %d", &year, &month)) != EOF)
	{
		switch (month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			printf("31\n");
			break;
		case 2:
			if (is_leap_year(year))
			{
				printf("29\n");
				break;
			}
			else
			{
				printf("28\n");
				break;
			}
		case 4:
		case 6:
		case 9:
		case 11:
			printf("30\n");
			break;
		default:
			break;
		}
	}

	return 0;
}

BC56 line segment pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print line segment patterns composed of "*".

Enter Description:
Multiple sets of inputs, an integer (1 ~ 100), representing the length of line segments, that is, the number of "".
Output Description:
For each line of input, the output occupies one line, and the line segment with corresponding length is composed of "".
Example 1
Input:
10
2
Output:
**********
**

Title Link

#include <stdio.h>

int main()
{
    int line = 0;
    while((scanf("%d", &line)) != EOF)
    {
        if(line >= 1 && line <=100)
        {
            while(line--)
            {
                printf("*");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC57 square pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print square patterns composed of "*".
Enter Description:
Multiple sets of inputs, an integer (1 ~ 20), represents the length of the square and the number of output lines.

Output Description:
For each line of input, output a square with corresponding side length composed of "", followed by a space.

Example 1
Input:
4
Output:
* * * *
* * * *
* * * *
* * * *
Example 2
Input:
5
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC58 right triangle pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print right triangle patterns composed of "".
Enter Description:
Multiple groups of inputs, an integer (2 ~ 20), represents the length of the right angle side of the right triangle, that is, the number of "" and the number of output lines.
Output Description:
For each line of input, output a right triangle of corresponding length composed of "", with a space after each "".
Example 1
Input:
4
Complex output:
*
* *
* * *
* * * *
Example 2
Input:
5
Output:
*
* *
* * *
* * * *
* * * * *

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<=i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC59 flip right triangle pattern

Multiple groups of inputs, an integer (2 ~ 20), represents the length of the right angle side of the inverted right triangle, that is, the number of "" and the number of output lines.
Output Description:
For each line of input, output a flipped right triangle of corresponding length composed of "*", with a space after each "*".
Example 1
Input:
5
Output:
* * * * *
* * * *
* * *
* *
*
Example 2
Input:
6
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n-i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC60 right triangle with space

describe
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print right triangle patterns with spaces composed of "*".

Enter Description:
Multiple groups of inputs, an integer (2 ~ 20), represents the length of the right angle side of the right triangle, that is, the number of "*" and the number of output lines.

Output Description:
For each line of input, output a right triangle of corresponding length composed of "", with a space after each "".

Title Link

#include <stdio.h>

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

BC61 pyramid pattern

describe
KiKi learned the cycle. Teacher BoBo gave him a series of practice of printing patterns. The task is to print pyramid patterns composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20), represents the length of pyramid edges, that is, the number of "" and the number of output lines.
Output Description:
For each line of input, the output is a pyramid composed of "", with a space after each "".

Title Link

#include <stdio.h>

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

BC62 flip pyramid pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the inverted pyramid pattern composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20), represents the length of flipped pyramid edges, that is, the number of "" and the number of output lines.
Output Description:
For each line of input, the output is a pyramid composed of "", with a space after each "".

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i =0;
        for(i=0; i<n; i++)
        {
            int j =0;
            for(j=0; j<i; j++)
            {
                printf(" ");
            }
            for(j=0; j<n-i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC63 diamond pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print diamond patterns composed of "*".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20).

Output Description:
For each line of input, the output is a diamond composed of "", with a space after each "".

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        int j = 0;
        for(i=0; i<=n; i++)
        {
            for(j=0; j<n-i; j++)
            {
                printf(" ");
            }
            for(j=0; j<=i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<=i; j++)
            {
                printf(" ");
            }
            for(j=0; j<n-i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
        
    }
    
    return 0;
}

BC64 K-shaped pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print K-shaped patterns composed of "*".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20).

Output Description:
For each line of input, the output is a K-shape composed of "", with a space after each "".
Example 1
Input:
2
Output:
* * *
* *
*
* *
* * *
Example 2
Input:
3
Output:
* * * *
* * *
* *
*
* *
* * *
* * * *
Example 3
Input:
4
Output:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

Title Link

#include <stdio.h>

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

BC65 arrow pattern

describe
KiKi learned the cycle. Teacher BoBo gave him a series of practice of printing patterns. The task is to print arrow patterns composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20).
Output Description:
For each line of input, the output is an arrow composed of "".

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        int j = 0;
        for(i=0; i<=n; i++)
        {
            for(j=0; j<n-i; j++)
            {
                printf("  ");
            }
            for(j=0; j<=i; j++)
            {
                printf("*");
            }
            printf("\n");
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<=i; j++)
            {
                printf("  ");
            }
            for(j=0; j<n-i; j++)
            {
                printf("*");
            }
            printf("\n");
        }
    }
    
    return 0;
}

BC66 backslash pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the backslash pattern composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20), represents the number of output lines and the number of "" that make up the backslash.

Output Description:
For each line of input, the output is a backslash composed of "*".

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<i; j++)
            {
                printf(" ");
            }
            printf("*");
            printf("\n");
        }
    }
    
    return 0;
}

BC67 slash pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of practice of printing patterns. The task is to print the forward slash pattern composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20), represents the number of output lines and the number of "" that make up the forward slash.
Output Description:
For each line of input, the forward slash composed of "*" is output.

Title Link

#include <stdio.h>

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

BC68 X pattern

describe
KiKi learned the cycle. Teacher BoBo gave him a series of practice of printing patterns. The task is to print X-shaped patterns composed of "".
Enter Description:
Multiple sets of inputs, an integer (2 ~ 20), represents the number of output lines, and also represents the length of backslash and forward slash constituting "X".
Output Description:
For each line of input, the X-shaped pattern composed of "" is output.

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) !=  EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                if(j==i || j+i==n-1)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

BC69 hollow square pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print "hollow" square patterns composed of "".
Enter Description:
Multiple sets of inputs, an integer (3 ~ 20), represents the number of output lines and the number of "" that make up the square edge.
Output Description:
For each line of input, output a "hollow" square composed of "", with a space after each "".

Title Link

#include <stdio.h>

/*
int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        for(i=0; i<=n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}
*/
int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        for(i=1; i<=n; i++)
        {
            int j = 0;
            for(j=1; j<=n; j++)
            {
                if(i==1 || i==n || j==1 || j==n)
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
    }

    return 0;
}

BC70 hollow triangle pattern

describe
KiKi learned the cycle, and teacher BoBo gave him a series of exercises to print patterns. The task is to print "hollow" triangular patterns composed of "".
Enter Description:
Multiple sets of inputs, an integer (3 ~ 20), represents the number of output lines and the number of "" that make up the triangle edges.
Output Description:
For each line of input, output a "hollow" triangle composed of "", with a space after each "".

Title Link

#include <stdio.h>

int main()
{
    int n = 0;
    while((scanf("%d", &n)) != EOF)
    {
        int i = 0;
        for(i=0; i<n; i++)
        {
            int j = 0;
            for(j=0; j<n; j++)
            {
                if(j==0 || i==n-1 || i==j)
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
            printf("\n");
        }
    }
    
    return 0;
}

Topics: C