Problem solving of ACM freshmen in Jiangzhong on October 26

Keywords: OJ

catalogue

Question 1: small ball free fall

Question 2: Super Mary game

Question 3: the algorithm improves the solution of binary linear equations

Question 4: number of daffodils

Question 5: C language - monkeys eat peaches

Question 6: hollowed out triangle

Question 7: find the greatest common divisor in exercise 2-4-7

Question 8: Che Shen Xihe

Question 9: A+B input and output exercise II

Question 10: late A+B

Question 1: small ball free fall

describe

A small ball falls freely from the height of 100m. After each landing, it jumps back to half of the original height, falls and bounces back. Find out how many meters it passes when it lands on the ground for the nth time and how high it bounces back for the nth time.

input

A positive integer n

output

Output one line.

Output two real numbers separated by spaces, and the result is accurate to two decimal places.

Input sample 1

1

Output sample 1

100.00 50.00

Problem solution

The Title Description proposes that each rebound is half of the original height, and the height is fixed at 100. Then the moment before the rebound, the small ball needs to fall first, so there is at least 100, and then sigh is half of the previous one, so the first fall should be 50. At the same time, before each fall, it needs to bounce up and fall first, so it needs to fall height * 2, and then it can be solved

code

//C language
#include <stdio.h>

int n;
int main()
{
    scanf("%d",&n);
    double sum=100.00;//The distance traveled by the ball
    double ract=50.00;//Rebound height
    for(int i=1;i<n;i++)
    {
        sum+=ract*2;
        ract/=2;
    }
    printf("%.2lf %.2lf\n",sum,ract);
    return 0;
}

Question 2: Super Mary game

describe

Super Mary game

input

                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############

output

Equivalent to input

Input sample 1

nothing

Output sample 1

nothing

Problem solution

Simple direct output, direct copy, and then one output for each line

code

//C + + code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    cout << "                ********" << endl;
    cout << "               ************" << endl;
    cout << "               ####....#." << endl;
    cout << "             #..###.....##...." << endl;
    cout << "             ###.......######              ###            ###"<< endl;
    cout << "                ...........               #...#          #...#" << endl;
    cout << "               ##*#######                 #.#.#          #.#.#" << endl;
    cout << "            ####*******######             #.#.#          #.#.#" << endl;
    cout << "           ...#***.****.*###....          #...#          #...#" << endl;
    cout << "           ....**********##.....           ###            ###" << endl;
    cout << "           ....****    *****...." << endl;
    cout << "             ####        ####" << endl;
    cout << "           ######        ######" << endl;
    cout << "##############################################################" << endl;
    cout << "#...#......#.##...#......#.##...#......#.##------------------#" << endl;
    cout << "###########################################------------------#" << endl;
    cout << "#..#....#....##..#....#....##..#....#....#####################" << endl;
    cout << "##########################################    #----------#" << endl;
    cout << "#.....#......##.....#......##.....#......#    #----------#" << endl;
    cout << "##########################################    #----------#" << endl;
    cout << "#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#" << endl;
    cout << "##########################################    ############" << endl;
    return 0;
}

Question 3: the algorithm improves the solution of binary linear equations

describe

Given a system of binary primary equations, the form is as follows:
  a * x + b * y = c;
  d * x + e * y = f;
X and Y represent unknowns. A, B, C, D, e and F are parameters.
Solve x,y

input

Enter Description:
The input contains six integers: a, b, c, d, e, f;
Input example:
Example:
3 7 41 2 1 9

output

Output Description:
The output is the solution of the system of equations, two integers x, y.
Output example:
Example:
2 5

Input sample 1

3 7 41 2 1 9

Output sample 1

2 5

TIPS

HINT: time limit: 1.0s memory limit: 256.0MB
  0 <= a, b, c, d, e, f <= 2147483647

Problem solution

This is a mathematical problem. First eliminate a variable X, or Y. I choose to eliminate X first

Equation:

Multiply the first expression as a wholeobtain

Then subtract the second from the first to get

Obtained by simplification

Then take y back to get x

code

//C language
#include <stdio.h>

int n;
int main()
{
    int a,b,c,d,e,f;
    scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
    int up=c*d-a*f;
    int down=b*d-a*e;
    int y=up/down;
    int x=(c-b*y)/a;
    printf("%d %d\n",x,y);
    return 0;
}

Question 4: number of daffodils

describe

Spring is the season of flowers, and daffodils are the most charming representatives. Mathematically, there is a number of daffodils, which he defines as follows:
"Narcissus number" refers to a three digit number, and the cubic sum of its digits is equal to itself, such as 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3.
Now it is required to output the number of daffodils in the range of m and n.

input

Two integers m and n (100 < = m < = n < = 999).

output

It is required to output the number of daffodils within a given range, that is, the number of daffodils to be output must be greater than or equal to m and less than or equal to n. if there are multiple daffodils, it is required to be output in a row from small to large, separated by a space;


Input sample 1

300 380

Output sample 1

370 371

Problem solution

The number of daffodils as the title says, if the sum of the cubes of each person is equal to himself, it is the number of daffodils. Then you need to take out each one first. First, the data we define is an integer. According to the direct rounding mechanism of C Language Division, we can remove the last bit by dividing by 10. As for the number of bits, we need to use the remainder operator (%), which is the remainder of the original data divided by a number

Example:

51 / 2 = 15 instead of 25.5

51% 2 = 1 because 51 = 2 * 25 + 1;

Then, you only need to take the remainder of 10 every time to get the last bit, and then eliminate the last bit by 10 to get each bit. You know that the number is 0, and then calculate a cube on the way.

For example:

153

The first time 153% 10 = 3 gets 3 153 / 10 = 15

The second time 15% 10 = 5 gets 5 15 / 10 = 1

The third time 1% 10 = 1 gets 1 1 / 10 = 0

Stop calculation

code

//C language
#include <stdio.h>

int n,m;

int func(int a)//Judge whether it is the number of daffodils, and return 1 to determine whether it is the number of daffodils
{
    int x=a;
    int num=0;
    while(x)
    {
        int tmp=x%10;//Take out each digit
        num+=(tmp*tmp*tmp);//Accumulate each digit cube
        x=x/10;
    }
    if(a==num)
        return 1;
    else
        return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    int ans=0;//Count the number of daffodils
    for(int i=n; i<=m; i++)
    {
        if(func(i))
        {
            ans=ans+1;
            printf("%d ",i);
        }

    }
    if(ans)//If the number is 0, no is output
        printf("\n");
    else
        printf("no\n");
    return 0;
}

Question 5: C language - monkeys eat peaches

describe

Monkeys eat peaches. On the first day, the monkey picked several peaches and ate half of them immediately. It was not fun, so he ate another one. The next morning, he ate half of the remaining peaches and another one. After that, I ate the remaining half and one every morning. When I wanted to eat again in the morning of the nth day, I saw that there was only one peach left. Ask how many peaches you pick on the first day.

input

N

output

Monkeys eat peaches. On the first day, the monkey picked several peaches and ate half of them immediately. It was not fun, so he ate another one. The next morning, he ate half of the remaining peaches and another one. After that, I ate the remaining half and one every morning. When I wanted to eat again in the morning of the nth day, I saw that there was only one peach left. Ask how many peaches you pick on the first day.

Input sample 1

10

Output sample 1

1534

Problem solution

The goal book says that monkeys eat more than half of one at a time, and then the answer needs to be operated in the opposite direction

Namely:

Now we know y is x, so

Then it can be calculated circularly according to this formula

code

//C language
#include <stdio.h>

int main()
{
    int n;
    int sum=1;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        sum=(sum+1)*2;
    }
    printf("%d\n",sum);
    return 0;
}

Question 6: hollowed out triangle

describe

Hollowing out a character triangle can save material cost and reduce weight, but the key is to pursue another visual effect. In the process of design, we need to give triangular templates of various pattern materials and sizes, which can be made temporarily by computer in order to see the effect.

input

Each line contains a character and an integer n (0 < n < 41). Different characters represent different patterns, and integer n represents the height of isosceles triangle. Obviously, the length of its bottom edge is 2n-1. If the @ character is encountered, it means that the template triangle is enough.

output

Each template triangle should have a blank line between them, and the middle of the triangle should be empty. Obviously, there are no extra spaces at the end of the line.

Input sample 1

X 2
A 7
@

Output sample 1

 X
XXX

      A
     A A
    A   A
   A     A
  A       A
 A         A
AAAAAAAAAAAAA

analysis

This question states in advance that multiple sets of inputs are required

Multi group input means that you can input until the input stop of the topic is reached

Multi group input C language format

If there is no requirement for multiple groups of input, it is: while(~scanf()) ~ indicates that the end of the file is known

If yes, you can choose to write in the loop body. The main format is while() to access an input statement

The title description is isosceles triangle, so it must be axisymmetric, and the bottom surface is 2*n-1, so the position of the central axis is n. then, the above is obtained according to symmetry, and the distance between the top and bottom letters and the central axis is 0~n-1, and then the result can be obtained.

Note that there is a new line after each triangle, which needs to be written well

code

//C language
#include <stdio.h>

int main()
{
    char a;
    int n;
    while(scanf("%c",&a))
    {

        if(a=='@')
            break;
        scanf("%d",&n);
        getchar();
        for(int i=0; i<n; i++)
        {
            if(i==n-1)
            {
                for(int j=0; j<n*2-1; j++)
                    printf("%c",a);
            }
            else
            {
                for(int j=1; j<=n+i; j++)
                {
                    if(j==n-i||j==n+i)
                        printf("%c",a);
                    else
                        printf(" ");
                }
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

Question 7: find the greatest common divisor in exercise 2-4-7

describe

Exercise 2-4-7 finding the greatest common divisor

input

Positive integers m and n separated by two spaces.

output

The greatest common divisor of m and n. Note the line wrap at the end of the line.

Input sample 1

35 14

Output sample 2

7

source

C language fourth edition teaching experiment - Chapter 2 algorithm

Problem solution

The greatest common factor can be obtained by rolling division

That is, a large number and a small number (not greater than the previous one) find the maximum common factor, so that the large number is complementary to the small number every time. When it can be positive, that is, the remainder is 0, it means that the latter number is the maximum common factor of this.

If space is limited, it will not be proved here. It must be proved next time.

The approximate process is

35 14

First 35% 14 = 7 to get 7

Keep 14 and 7, that is, the large number is 14 and the small number is 7

I.e. 14% 7 = 0

So the maximum common factor is 7

code

#include <stdio.h>

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    if(n<m)
    {
        int tmp=n;
        n=m;
        m=tmp;
    }
    while(1)
    {
        if(n%m==0)
        {
            printf("%d\n",m);
            break;
        }
        int tmp=m;
        m=n%m;
        n=tmp;
    }
    return 0;
}

Question 8: Che Shen Xihe

describe

In the middle of the great wilderness, there is a Tiantai Mountain, into which the sea water flows from the south. Outside the East China Sea, between GaN and Shui, there is a state of Xihe. Here is a woman named Xihe, the wife of emperor Jun, who will bathe the sun in Gan yuan. Xihe gave birth to ten suns, and later Houyi shot nine!

Xihe loves the rest of the sun very much. He drives around the Xihe driveway with the sun every day, resulting in sunrise and sunset, day and night.

One day, the sun proposed to drive by himself and had a competition with Xihe.

Although Xihe loved the sun very much, he also wanted to witness the growth of the sun, so he gladly accepted the challenge of the sun. The challenge requirements and process are as follows:

1. The two must compete in Xihe lane;

2. Xihe lane is circular. The speed of two people in the lane can be different, but each person must keep driving at a constant speed;

3. They started from the starting point in the opposite direction at the same time. After A minute, the two cars met on the road and passed by;

4. After another B minutes, one person has returned to the end (also the starting point). At this time, the distance between the other person and the end point is C;

5. The one who reaches the finish line first wins.

At the end of the challenge, the people who reached the end first walked away, and the real car God never appeared in front of people. The sun arrived at the end, looked at the shadow of the chariot God Xihe who was going down the mountain, and suddenly understood the real meaning of the chariot God. In order not to leave regret, the sun is determined to calculate the length of Xihe lane. Can you help him realize this wish?

input

The input has multiple groups (no more than 100) of test instances.

Each group of test examples occupies one line and is three positive integers A (1 ≤ A ≤ 200), B (1 ≤ B ≤ 200) and C (1 ≤ C ≤ 200). The meaning is shown in the question.

The end of input will be represented by A test instance with A line A, B and C equal to 0, which should not be processed.

Note: the test example ensures that the input data is legal and the output data must be a positive integer.

output

Each group of test cases outputs one line, which is a positive integer, that is, the length of Xihe lane.

Input sample 1

120 90 30
100 50 50
0 0 0

Output sample 1

120
100

source

The Fifth National College Students' program design competition of traditional Chinese Medicine

Problem solution

Three pieces of information can be extracted according to the requirements of the topic

1. The faster man reached the end in A+B time

2. The slower person's total mileage is (A+B) * speed + C

3. Two person turn at time A, i.e. A * (speed 1 + speed 2) = mileage

Three equations can be obtained (X is the speed of faster people, Y is the speed of slower people, and L is the length)

1  A*(X+Y)=L

2  X*(A+B)=L

3  (A+B)*Y+C=L

Equation 4 is obtained from 1    

Add 2 and 3 to get equation 5  

Then subtraction and simplification can be obtained

Because we don't know the size relationship between A and B, we need to take the absolute value

code

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

int main()
{
    int A,B,C;
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        if(A==0&&B==C&&A==B)
            break;
        int way=(A*C)/(abs(A-B));//abs is the absolute value
        printf("%d\n",way);
    }
    return 0;
}

Question 9: A+B input and output exercise II

describe

Your task is to calculate a+b.

input

The first line is an integer N, indicating that there will be N lines a and b, separated by spaces.

output

For each pair of a and b entered, you need to output the sum of a and b on the corresponding line.

As for the second pair a and b, the corresponding sum is also output in the second line.

Input sample 1

2
1 5
10 20

Output sample 1

6
30

Problem solution

This problem is to input N a and b and then output a+b

code

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

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}

Question 10: late A+B

describe

"A+B" is a familiar topic. Before the start of this school competition, CHZ classmate (called "dp Little Prince") always asked me to start A+B to do it for him, but he felt very sorry that he failed to do it for various reasons. In order to make up for his regret before the end of the world, he decided to start A+B to give it to CHZ classmate!! the topic is as follows:

        First give you an array a of n (1 = < n < = 100000), and then give you two numbers X, y (1 = < X, y < n). Find sum=(A0+A1 +... + Ax-1)+(Ay+Ay+1 +... + An-1);

input

There are multiple groups of data. The first row of each group of data has two numbers n, m (1 = < m < = 50000), indicating the number of data and the number of queries. The second row is n data. From the third row to m+3 rows, each row has two numbers X, Y (1 = < X, Y < n);

output

One result is output for each query, and each result occupies one row

Input sample 1

10 1
1 2 3 4 5 5 4 3 2 1
5 5

Output sample 1

30

Problem solution

The title means to give you a set of data, and then find the sum of the first X numbers and the last Y numbers

You can use an array to store the sum of the first few numbers and the sum of the last few numbers

Then add and output

code

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

int a[100001];
int sum_x[100001];
int sum_y[100001];

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int i;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum_x[i]=a[i];//Consider the previous values
            if(i>0)//Array subscript must be greater than 0
                sum_x[i]+=sum_x[i-1];

        }
        for(i=n-1;i>=0;i--)
        {
            sum_y[i]=a[i];
            if(i<n-1)//The following table of the array must be less than n
                sum_y[i]+=sum_y[i+1];
        }
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>0)//Array subscript must be greater than 0
            printf("%d\n",sum_x[a-1]+sum_y[b]);
            else
                printf("%d\n",sum_x[a]+sum_y[b]);
        }

    }
    return 0;
}

Posted by saeed_violinist on Wed, 27 Oct 2021 05:12:50 -0700