# C Problem Set 44-52

44. Calculate the unit step function

45. Triangle Judgment

46. ​​Measure the degree of body fat and thin

47. Calculating quadratic equations in one variable

48. Get the number of days in the month

49. Simple Calculator

50. Line pattern

51. Square pattern

52. Right triangle pattern

## 44. Calculate the unit step function

Title description:

KiKi recently studied the signal and system course. In this course, there is a very interesting function, the unit step function, one of which is defined as: Now try to find the value of the unit impulse function in the time domain t.

Enter a description:

```The topic has multiple sets of input data, one for each line t(-1000<t<1000)represents the time domain of the function t.

Input example:
11
0
-11```

Output description:

```Output the value of the function on a newline.

Example output:
1
0.5
0```

Parse:

For this question, you only need to pay attention to multiple sets of inputs.

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int t = 0;
while (~scanf("%d", &t))
{
if (t > 0)
{
pritnf("%d\n",1);
}
else if (t == 0)
{
printf("%.1lf", 0.5);
}
else
{
printf("%d\n", 0);
}
}
return 0;
}```

## 45. Triangle Judgment

Title description:

KiKi wants to know whether the three sides a, b, and c already given can form a triangle, and if they can form a triangle, determine the type of triangle (equilateral triangle, isosceles triangle or ordinary triangle).

Enter a description:

```There are multiple sets of input data in the title, and each line enters three a，b，c(0<a,b,c<1000)，
As the three sides of the triangle, separated by spaces.

Input example:
2 3 2
3 3 3```

Output description:

```For each set of input data, the output occupies one row. If a triangle can be formed,
An equilateral triangle outputs " Equilateral triangle!"，
isosceles triangle then output " Isosceles triangle!"，
The rest of the triangles output " Ordinary triangle!"，
Otherwise output " Not a triangle!".

Example output:
Isosceles triangle!
Equilateral triangle!```

Parse:

For this topic, we only need to be clear about the judgment rules of triangles, and then we need to exhaust them one by one.

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
while(~scanf("%d %d %d", &a, &b, &c))
{
//Determine whether it is a triangle
if ((a + b > c) && (a + c > b) && (b + c > a))
{
//is a triangle

//Equilateral triangle
if (a == b && b == c)
printf("Equilateral triangle!");
else if ((a == b && a != c) || (a == c && c != b) || (c = b && b != a))
printf("Isosceles triangle!");
else
printf("Ordinary triangle!");
}
else
printf("Not a triangle!");
}
return 0;
}
```

## 46. ​​Measure the degree of body fat and thin

Title description:

Based on the case of calculating BMI (BodyMassIndex, body mass index), judge the degree of body fat and thin. The BMI China standard is shown in the table below. Enter a description:

```Multiple sets of input, each line includes two integers, separated by spaces, which are weight (kg) and height (cm).

Input example:
80 170
60 170
90 160
50 185```

Output description:

```For each line of input, the output is a line, the degree of body fat or thin, that is, classification.

Example output:
Overweight
Normal
Obese
Underweight```

Parse:

Floating-point number division must ensure that at least one of the two ends of the division sign is a floating-point number.

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
double weight = 0.0;
double high = 0.0;
double bmi = 0.0;
while (~scanf("%lf %lf", &weight, &high))
{
bmi = weight / ((high / 100.0) * (high / 100.0));
if (bmi < 18.5)
printf("Underweight");
else if (bmi >= 18.5 && bmi <= 23.9)
printf("Normal");
else if (bmi > 23.9 && bmi <= 27.9)
printf("Overweight");
else
printf("Obese");
}
return 0;
}```

## 47. Calculating quadratic equations in one variable

Title description:

Input the values ​​of a, b, c from the keyboard, program to calculate and output the root of the quadratic equation axe2 + bx + c = 0, when a = 0, output "Not quadratic equation", when a ≠ 0, according to △ The three cases of = be2 - 4*a*c calculate and output the roots of the equation.

Enter a description:

```Multiple sets of input, one line, containing three floating point numbers a, b, c，
separated by a space,
Represents a quadratic equation in one variable axe2 + bx + c = 0 coefficient.

Example input: 2.0 7.0 1.0```

Output description:

```For each set of inputs, output one row, and output a quadratic equation in one variable ax2 + bx +c = 0 The case of the root.
if a = 0，output " Not quadratic equation"；
if a ≠  0，There are three situations:

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

△  > 0，Then the two real roots are not equal, and the output form is: x1=...;x2=...，in x1  <=  x2.

△  < 0，Then there are two imaginary roots, then output: x1=Real-imaginary part i;x2=Real+imaginary part i，
Right now x1 The imaginary part coefficient of is less than or equal to x2 The imaginary part coefficient of , when the real part is 0, cannot be omitted.
Real= -b / (2*a),imaginary part= sqrt(-△ ) / (2*a)

All real numbers must be accurate to 2 digits after the decimal point, and there should be no spaces between numbers and symbols.

Example output: x1=-3.35;x2=-0.15```

Parse:

1. Multiple sets of input

2. Give corresponding calculation results according to different constraints

3. There is a limit on the number of digits after the decimal point

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
int main()
{
double a = 0.0;
double b = 0.0;
double c = 0.0;
while ((scanf("%lf %lf %lf", &a, &b, &c)) != EOF)
{
if (a == 0.0)
{
}
else
{
double disc = b * b - 4 * a * c;
if (disc == 0.0)
{
//has two equal real roots
printf("x1=x2=%.2lf", -b / (2 * a));
}
else if (disc > 0.0)
{
//has two unequal real roots
printf("x1=%.2lf;x2=%.2lf", (-disc - b) / (2 * a), (disc - b) / (2 * a));
}
else
{
//has two unequal imaginary roots
double real = -b / (2 * a);
double image = sqrt(-disc) / (2 * a);
printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi", real, image, real, image);
}
}
}
return 0;
}```

## 48. Get the number of days in the month

Title description:

KiKi wants to get how many days there are in a certain month in a certain year, please help him program it. Enter a year and a month to calculate how many days there are in this year and month.

Enter a description:

```Multiple sets of input, one line has two integers, representing year and month respectively, separated by spaces.

Example input: 2023 4```

Output description:

```For each set of inputs, the output is one line, an integer indicating how many days there are in this year and month.

Example output:```

Parse:

Judgment of leap year:

1. Divisible by 4 but not divisible by 100

2. Divisible by 400

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int year = 0;
int month = 0;
int days = { 31,28,31,30,31,30,31,31,30,31,30,31 };
while ((scanf("%d %d", &year, &month))!=EOF)
{
int day = days[month - 1];
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (month == 2)
{
day += 1;
}
}
printf("%d\n", day);
}
return 0;
}
```

## 49. Simple Calculator

Title description:

KiKi implements a simple calculator to realize the "addition, subtraction, multiplication and division" operation of two numbers. The user inputs the formula "operand 1 operator operand 2" from the keyboard, calculates and outputs the value of the expression, if the input operation symbol does not include In the range (+, -, *, /), "Invalid operation!" is output. When the operator is a division operation, i.e. "/". If operand 2 is equal to 0.0, output "Wrong!Division by zero!"

Enter a description:

```Multiple sets of input, one row, operand 1 operator operand 2
(The operation symbols include four types:+,-,*,/).

Input example:
1.0+3.0
1.0;4.0
44.0/0.0```

Output description:

```For each set of inputs, the output is one line.

If both operands and operands are legal, output an expression,
operand 1 operator operand 2=operation result,
Each number has 4 digits after the decimal point, and there is no space between the number and the symbol.

If the operator entered is not included in (+,-,*,/)range,
output " Invalid operation!".
When the operator is a division operation, that is, "/"hour.

if operand 2 is equal to 0.0，then output " Wrong!Division by zero!".

Example output:
1.0000+3.0000=4.0000
Invalid operation!
Wrong!Division by zero!```

Parse:

In this question, don't forget that there is a break statement after the case statement.

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
double num1 = 0.0;
double num2 = 0.0;
char op = 0;
while (~scanf("%lf %c %lf", &num1, &op, &num2))
{
switch (op)
{
case '+':
printf("%.4lf+%.4lf=%.4lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.4lf-%.4lf=%.4lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.4lf*%.4lf=%.4lf", num1, num2, num1 * num2);
break;
case '/':
if (num2 == 0)
printf("Wrong!Division by zero!");
else
printf("%.4lf/%.4lf=%.4lf", num1, num2, num1 / num2);
break;
default:
printf("Invalid operation!");
break;
}
}
return 0;
}```

## 50. Line pattern

Title description:

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

Enter a description:

```Multiple input, an integer (1~100)，Indicates the length of the line segment, that is, "*"quantity.

Input example:
10
2```

Output description:

```For each line of input, the output occupies one line, use "*"composed of line segments of corresponding length.

Example output:
**********
**```

Parse:

1. Multiple sets of input

2. Print one * each time, just print according to the number entered and then change the line

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n = 0;
while ((scanf("%d", &n)) != EOF)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("*");
}
printf("\n");
}
return 0;
}```

## 51. Square pattern

Title description:

KiKi learned the cycle, and Mr. BoBo gave him a series of pattern printing exercises. The task was to print a square pattern composed of "*".

Enter a description:

```Multiple input, an integer (1~20)，
Indicates the length of the square and also the number of output lines.

Input example:
4```

Output description:

```For each line of input, output with "*"A square of corresponding side length is formed,
each "*"followed by a space.

Example output:
* * * *
* * * *
* * * *
* * * *```

Parse:

1. Multiple sets of input

2. Each group is composed of n+ spaces

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n = 0;
while ((scanf("%d", &n)) != EOF)
{
int i = 0;
for (i = 0; i < n; i++)
{
//print the contents of a line
int j = 0;
for (j = 0; j < n; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}```

## 52. Right triangle pattern

Title description:

KiKi learned the cycle, and Teacher BoBo gave him a series of exercises for printing patterns. The task is to print a right-angled triangle pattern composed of "*".

Enter a description:

```Multiple sets of inputs, an integer (2~20)，Indicates the length of the right-angled side of a right-angled triangle, that is, "*"The number of , also indicates the number of output lines.

Input example:
4```

Output description:

```For each line of input, output with "*"A right-angled triangle of corresponding length is formed,
each "*"followed by a space.

Example output:
*
* *
* * *
* * * *```

Parse:

1. Multi-group output

2. The output of each line is related to the value of i, as long as you master the rules, it is very easy.

code:

```#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n = 0;
while ((scanf("%d", &n)) != EOF)
{
int i = 0;
for (i = 0; i < n; i++)
{
//print the contents of a line
int j = 0;
for (j = 0; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}```

Tags: C

Posted by aris1234 on Thu, 06 Apr 2023 08:01:44 +0530