Niuke.com-Introduction to Programming for Beginners-BC21~BC30

BC21 floating point numbers

describe

Given a floating point number, ask to get the single digit of the floating point number.

Enter description:

A line, including a float.

Output description:

A line containing an integer, the single digit corresponding to the input floating-point number.

Example 1

Input: 13.141

output: 1

important point:

For floating-point types, cast int will discard the fractional part.

#include <stdio.h>
int main(void)
{
    int a;
    scanf("%d",&a);
    printf("%d",a%10);
    return 0;
}

BC22 How many seconds can you live

describe

 Question: There are about 3.156×107s in a year, ask to enter your age, show how many seconds in that age.

Enter description:

A line, including an integer age (0<age<=200).

Output description:

A line containing an integer that prints the age in seconds.

Example 1

Input: 20

Output: 631200000

Note: The output data size must match the corresponding type, otherwise the result will be inaccurate. Such as float a = 0 and float a = 0.0 in the final result is different.

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

int main(void)
{
    unsigned a;
    scanf("%d", &a);
    a = a * 3.156 * pow(10,7);
    printf("%ld\n", a);
    
    return 0;
}

BC23 time conversion

describe

Given the number of seconds seconds (0 < seconds < 100,000,000), convert seconds to hours, minutes, and seconds.

Enter description:

A line containing an integer, the given number of seconds.

Output description:

A line containing three integers, followed by the hours, minutes, and seconds (possibly zero) corresponding to the input integers, separated by a space.

Example 1

Input: 3661

Output: 1 1 1

important point:

without

#include<stdio.h>

int main(void)
{
    int a;
    int b, c, d, e;
    scanf("%d", &a);
    b = a / 3600;
    c = a % 3600;
    d = c / 60;
    e = c % 60;
    printf("%d %d %d", b, d, e);
    
    return 0;
}

Average Calculation of BC24 Overall Score

describe

Input the grades of 3 subjects of a student in turn, and output the student's total grade and average grade on the screen.

Enter description:

One line, 3 subject scores, separated by a space.

Output description:

One line, total and average grades (with two decimal places), separated by a space.

Example 1

Input: 79.5 80.0 98.0

Output: 257.50 85.83

important point:

Pay attention to the type of data, if the floating point type is output with %d, the decimal part will be discarded. The sum can be represented by its English sum.

#include<stdio.h>

int main(void)
{
    float a, b, c, d, e;
    scanf("%f %f %f", &a, &b, &c);
    d = a + b + c;
    e = d / 3;
    printf("%.2f %.2f", d, e);
    return 0;
    
}

BC25 Calculate Body Mass Index

describe

 Problem: Calculate BMI (Body Mass Index). BMI (Body Mass Index, referred to as Body Mass Index, also known as Body Weight , English for Body Mass Index, referred to as BMI) is a number obtained by dividing the weight in kilograms by the square of the height in meters. A measure of thinness and health. Mainly used for statistical purposes, when we need to compare and analyze the health effects of a person's weight on people of different heights, the BMI value is a neutral and reliable indicator .

Enter description:

One line, two integers, representing weight (kg) and height (cm) respectively, separated by a space.

Output description:

One line, BMI index (with two decimal places).

Example 1

Input: 70 170

output: 24.22

important point:

Sometimes the questions are long, but the results are simple. Sometimes life is short, but the road is wonderful.

#include<stdio.h>

int main(void)
{
    float a, b;
    scanf("%f %f", &a, &b);
    printf("%.2f", a / ((b / 100) * (b / 100)));
    return 0;
}

BC26 Calculate the perimeter and area of ​​a triangle

describe

Calculate the perimeter and area of ​​the triangle given its 3 sides a, b, c (0 < a, b, c < 100,000).

Enter description:

One line, three sides of a triangle (which can form a triangle), separated by a space.

Output description:

One line, the perimeter and area of ​​the triangle (with two decimal places), separated by a space. For details of the output format, see the output example.

Example 1

Input: 3 3 3

输出:circumference=9.00 area=3.90

important point:

1. In this question, some people may not type the English word of perimeter incorrectly (o _ o);

2. Some people may not know that only the lengths of the three sides known to form a triangle are used to calculate the area. Here, the Qin Jiushao formula is used.

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

int main(void)
{
    double a, b, c;
    double d;
    scanf("%lf %lf %lf", &a, &b, &c);
    d = (a + b + c) / 2;
    printf("circumference=%.2lf area=%.2lf", a + b + c, sqrt( d * (d - a) * (d - b) * (d - c)));
    return 0;
}

BC27 Calculate the volume of a sphere

describe

Given the radius of a sphere, calculate its volume. where the formula for the volume of a sphere is V = 4/3*πr3, where π = 3.1415926.

Enter description:

One line, the radius of the sphere as a float.

Output description:

One line, the volume of the sphere, with 3 decimal places.

Example 1

Input: 3.0

output: 113.097

important point:

Remember what BC22's attention points said? Now change 4.0 to 4 and 3.0 to 3 and try what the result will be.

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

int main(void)
{
    double r;
    scanf("%lf", &r);
    printf("%.3lf", 4.0 / 3.0 * 3.1415926 * pow(r,3));
    
    return 0;
}

BC28 case conversion

describe

Convert uppercase and lowercase letters. Multiple sets of input and output.

Enter description:

Multiple sets of input, each line input capital letters.

Output description:

For each set of input and output the corresponding lowercase letter.

Example 1

Input: AB

output: ab

Remark:

In the multi-group input process, it should be noted that "carriage return" is also a letter, so the letter should be "absorbed" (getchar()).

important point:

1. See the title clearly: Multiple sets of input and output means that while(...);

2. Case conversion has its library function tolower in the c language, which is included in the header file ctype.h, but in order to be challenging, we still write a program ourselves without this function.

#include<stdio.h>
int main(void)
{
    char ch;
    while((ch = getchar()) != EOF)
    {
     if(ch >= 'A' && ch <= 'Z')
     {
         ch += 32;
         
     }
        putchar(ch);
    }
    return 0;
}

BC29 Calculation of the nth power of 2

describe

On the basis of not using cumulative multiplication, the calculation of the nth power of 2 is realized by the shift operation (<<).

Enter description:

Multiple sets of input, each line input integer n (0 <= n < 31).

Output description:

For each set of inputs, output the corresponding result of the nth power of 2.

Example 1

Input: 2 10

Output: 4 1024

important point:

Calculated by shifting <<, for integers, shifting by one bit to the left is *2.

#include <stdio.h>

int main (void)
{
    int n = 0;
    while (scanf("%d",&n)!= EOF)
    {
        printf("%d\n", 1 << n);
    }
    return 0;
}

BC30 KIKI and yogurt

describe

BoBo bought a box of yogurt, which contained n boxes of unopened yogurt. KiKi likes to drink yogurt, and found yogurt at the first time. KiKi can drink up one box of yogurt every h minutes, and KiKi won't drink another box of yogurt until m minutes have passed, how many unopened boxes of yogurt are left after m minutes?

Enter description:

Multiple sets of input, each set of input is only one line, including n, h and m (all integers). The input data guarantees that m <= n * h.

Output description:

For each set of input, the output is also only one line, and the number of unopened yogurt boxes remaining.

Example 1

Input: 8 5 16

output: 4

important point:

The difficulty of this question is still there, and those who can't think of it can write a few numbers to try it out. If you really can't, you can observe other people's code and internalize it into your own.

#include<stdio.h>

int main(void)
{
    int n, h, m;
    while(scanf("%d %d %d", &n, &h, &m) != EOF)
    {
        if(m % h != 0)
        {
            n = n - ((m / h) + 1);
        }
        else
        {
            n = n - (m / h);
        }
        printf("%d\n", n);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324115033&siteId=291194637