The sixth day of punch in learning on November 28, 2021 (Application of structure)

Keywords: C linear algebra

Structure can systematically link several variables together, which can solve a lot of trouble in some examples.

Examples are as follows:

To register patients for medical treatment, prepare a procedure to arrange the registered patients in the order of medical treatment according to the following principles:

  1. Elderly (age ≥ 60)   Age) has priority over non elderly people.
  2. The elderly see a doctor in the order of age, and those with the same age are sorted in the order of registration.
  3. Non elderly people see a doctor in the order of registration.

Input format

The first   1 line, enter a value less than   one hundred   A positive integer representing the number of patients;

Later, input the information of a patient in each line according to the sequence of patient registration, including: a length less than   The string of 10 represents the patient's ID (each patient's ID is different and contains only numbers and letters), and an integer represents the patient's age (no more than)   one hundred   Years old), separated by a single space.

Output format

Output the patient ID according to the arranged medical order, one for each line.

Sample Input

5
021075 40
004003 15
010158 67
021033 75
102012 30

Sample Output

021033
010158
021075
004003
102012

Resolution code:

#include<stdio.h>
#include<string.h>
struct man
{
    char id[10];
    int age;
};
int main()
{
    struct man a[100];
    struct man young[100];
    struct man old[100];
    int n;
    char t[10];
    scanf("%d",&n);
    int i=0,r=0,y=0,e=0,f=0;
    for(i=0;i<n;i++)
       {
           scanf("%s %d",&a[i].id,&a[i].age);
           if(a[i].age>=60)
           {
               old[e]=a[i];
               e++;
           }
           else
           {
               young[f]=a[i];
               f++;
           }
       }
    for(i=0;i<e-1;i++)
    {
        for(r=0;r<e-1-i;r++)
        {
            if(old[r].age<old[r+1].age)
            {
               strcpy(t,old[r].id);
               y=old[r].age;
               strcpy(old[r].id,old[r+1].id);
               old[r].age=old[r+1].age;
               strcpy(old[r+1].id,t);
               old[r+1].age=y;
            }
        }
    }
    for(i=0;i<e;i++)
    printf("%s\n",old[i].id);
    for(i=0;i<f;i++)
    printf("%s\n",young[i].id);
    return 0;
}

This code can be optimized as follows:

for(i=0;i<e-1;i++)
    {
        for(r=0;r<e-1-i;r++)
        {
            if(old[r].age<old[r+1].age)
            {
               t=old[r];
               ald[r]=old[r+1];
               old[r+1]=t;
            }
        }
    }

And

for(i=0;i<e-1;i++)
    {
        for(r=0;r<e-1-i;r++)
        {
            if(old[r].age<old[r+1].age)
            {
               strcpy(t,old[r].id);
               y=old[r].age;
               strcpy(old[r].id,old[r+1].id);
               old[r].age=old[r+1].age;
               strcpy(old[r+1].id,t);
               old[r+1].age=y;
            }
        }
    }

Is equivalent. When I was doing it, I forgot.

Question 2 is the same:

A primary school recently received a sponsorship, and plans to take out some of them for the former students with excellent academic performance   five   Scholarships were awarded to students. At the end of the term, every student has   three   Grades of courses: Chinese, mathematics and English. First, sort according to the total score from high to low. If the total scores of the two students are the same, then sort according to the Chinese scores from high to low. If the total scores and Chinese scores of the two students are the same, the students with small student number are required to be in the front. In this way, the ranking of each student is uniquely determined.

Task: first according to the entered   three   Calculate the total score of each course, then sort according to the above rules, and finally output the student number and total score of the top five students according to the ranking order. Attention, before   five   Each student has different scholarships, so you must sort them strictly according to the above rules. For example, in a correct answer, if the output data of the first two lines (two numbers per line: student number and total score) is:

7   279

5   279

The meaning of these two lines of data is: the student numbers of the two students with the highest total scores are   seven   No. 5   No. the total score of these two students is   two hundred and seventy-nine   (the total score is equal to the sum of the input scores of Chinese, mathematics and English), but the language scores of students with student number 7 are higher. If your top two output data are:

5    279

7    279

It is treated as an output error and cannot be scored.

Input format

The first   eleven   The behavior is a positive integer   n. Indicates the number of students participating in the selection.

The first   twenty-two   reach   n+1   Line, each line has   Three numbers separated by spaces, each in   0   reach   Between 100 z   one   OK   three   A number in turn indicates that the student number is   j-1   The student number of each student is numbered according to the input order   1 ∼ n (exactly the line number minus the input data)   1).

The data given are correct and do not need to be tested.

Output format

share   five   Lines, each line is two positive integers separated by spaces, representing the first row in turn   five   Student number and total score of students.

Data range

50% of the data meet: the total scores of students are different.

100% data meet: 6 ≤ n ≤ 300.

Sample Input

6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

Sample Output

6 265
4 264
3 258
2 244
1 237

Sample Input 2

8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

Sample Output 2

8 265
2 264
6 264
1 258
5 258

code:

#include<stdio.h>
#include<string.h>
struct man
{
    char id[10];
    int age;
};
int main()
{
    struct man a[100];
    struct man young[100];
    struct man old[100];
    int n;
    char t[10];
    scanf("%d",&n);
    int i=0,r=0,y=0,e=0,f=0;
    for(i=0;i<n;i++)
       {
           scanf("%s %d",&a[i].id,&a[i].age);
           if(a[i].age>=60)
           {
               old[e]=a[i];
               e++;
           }
           else
           {
               young[f]=a[i];
               f++;
           }
       }
    for(i=0;i<e-1;i++)
    {
        for(r=0;r<e-1-i;r++)
        {
            if(old[r].age<old[r+1].age)
            {
               strcpy(t,old[r].id);
               y=old[r].age;
               strcpy(old[r].id,old[r+1].id);
               old[r].age=old[r+1].age;
               strcpy(old[r+1].id,t);
               old[r+1].age=y;
            }
        }
    }
    for(i=0;i<e;i++)
    printf("%s\n",old[i].id);
    for(i=0;i<f;i++)
    printf("%s\n",young[i].id);
    return 0;
}

Then I wrote three similar questions to practice hands:

I

Xiao Wang is the warehouse keeper of the company. One day, he received a task: find a steel pipe from the warehouse. It doesn't sound like much, but the requirements of the steel pipe really made him difficult. The requirements are as follows:

  1. This steel pipe must be the longest in the warehouse;
  2. This steel pipe must be the thinnest of the longest steel pipes;
  3. This steel pipe must conform to the largest code of the first two steel pipes (each steel pipe has a different code from each other. The larger the code, the closer the production date).

Relevant information is available, but manually select the one that meets the requirements from hundreds of steel pipe materials... Otherwise, please write a program to help him solve this problem.

Input format

The first line of the file is an integer n   (1 ≤ n ≤ 1000), indicating the quantity of all steel pipes in the warehouse.

after   nn   Row, three integers in each row, respectively representing the length of a steel pipe (in mm, in [110000]   Range), diameter (in mm, in [1100]   Range) and coding (one   nine   Bit integer).

Output format

only one   nine   Bit integer representing the code of the selected steel pipe.

Sample Input

4
3000 50 872198442
3000 45 752498124
2000 60 765128742
3000 45 652278122

Sample Output

752498124
#include<stdio.h>
struct iron
{
    int chang;
    int kuan;
    int id;
};
int main()
{
    struct iron a[1000];
    int n;
    int max_chang=0,min_kuan=1000,max_id=0;
    scanf("%d",&n);
    int i=0,r=0;
    for(i=0;i<n;i++)
        scanf("%d %d %d",&a[i].chang,&a[i].kuan,&a[i].id);
    for(i=0;i<n;i++)
    {
        if(a[i].chang>max_chang)
            max_chang=a[i].chang;
    }
    for(i=0;i<n;i++)
    {
        if(a[i].chang==max_chang)
        {
            if(a[i].kuan<min_kuan)
                min_kuan=a[i].kuan;
        }
    }
    for(i=0;i<n;i++)
    {
        if(a[i].chang==max_chang&&a[i].kuan==min_kuan)
        {
            if(a[i].id>max_id)
                max_id=a[i].id;
        }
    }
    printf("%d",max_id);
    return 0;
}

II

The selection of World Expo volunteers is in full swing in city A. in order to select the most suitable talents, city a has conducted a written test for all the registered contestants. Only those whose written test scores reach the interview score line can enter the interview. The interview score line is based on the number of people admitted according to the plan   150%   Delimitation, i.e. if you plan to admit   m   Volunteers, the interview score line is ranked No   m × The scores of 150% (rounded down) contestants, and the contestants who finally enter the interview are all those whose written test scores are not lower than the interview score line.

Now please write a program to delimit the interview score line, and output the registration number and written test results of all contestants entering the interview.

Input format

In the first line, two integers n,m(5 ≤ n ≤ 5000,3 ≤ m ≤ n) are separated by a space, where   n   Indicates the total number of contestants registered for the written examination, mm   Indicates the number of volunteers to be admitted. Enter the data to ensure M × 150%   Rounded down less than or equal to   n.

Second line to second line   n+1 line, each line includes two integers, separated by a space in the middle, which are the contestant's registration number respectively   k(1000≤k≤9999)   And the contestant's written test score s(1 ≤ s ≤ 100)  . The data ensure that the registration numbers of contestants are different.

Output format

First line, yes   two   An integer separated by a space. The first integer represents the interview score line; the second integer is the actual number of contestants entering the interview.

Starting from the second line, each line contains   two   An integer, separated by a space in the middle, respectively represents the registration number and written test scores of the contestants entering the interview. It is output from high to low according to the written test scores. If the scores are the same, it is output according to the order of registration number from small to large.

Example description

m × 150%=3 × 150%=4.5   Rounded down to   4. Guarantee   four   The score line for an individual to enter the interview is   88, but because   eighty-eight   There are multiple scores, so all scores are greater than or equal to   eighty-eight   All the contestants can enter the interview, so they will eventually have   five   Individuals enter the interview.

Sample Input

6 3
1000 90
3239 88
2390 95
7231 84
1005 95
1001 88

Sample Output

88 5
1005 95
2390 95
1000 90
1001 88
3239 88
#include<stdio.h>
struct man
{
    int id;
    int grade;
};
int main()
{
    struct man a[5000];
    struct man t;
    int n,m;
    scanf("%d %d",&n,&m);
    int i=0,r=0;
    for(i=0;i<n;i++)
        scanf("%d %d",&a[i].id,&a[i].grade);
    for(i=0;i<n-1;i++)
    {
        for(r=0;r<n-1-i;r++)
        {
            if(a[r].grade<a[r+1].grade)
            {
                t=a[r];
                a[r]=a[r+1];
                a[r+1]=t;
            }
        }
    }
    m=m*1.5;
    while(1)
    {
        if(a[m-1].grade==a[m].grade)
            m++;
        else
            break;
    }
    for(i=0;i<m-1;i++)
    {
        for(r=0;r<m-1-i;r++)
        {
            if(a[r].grade==a[r+1].grade&&a[r].id>a[r+1].id)
            {
                t=a[r];
                a[r]=a[r+1];
                a[r+1]=t;
            }
        }
    }
    printf("%d %d\n",a[m-1].grade,m);
    for(i=0;i<m;i++)
        printf("%d %d\n",a[i].id,a[i].grade);
    return 0;
}

III

Mr. garlic and his friends went to climb Xiangshan mountain. They were intoxicated by the beautiful scenery and wanted to take a group photo. If they stand in a row, all the boys are on the left (from the perspective of the photographer), and all the girls are on the right in the order from low to high, and all the girls are on the right in the order from high to low, what is the effect of their group photo (everyone's height is different)?

Input format

The first line is the number of people n(2 ≤ n ≤ 40, and at least   one   A boy and   1 girl).

Followed by   n   Line, enter a person's gender (male or female) and height (in the range of   Floating point numbers in [0,2], in meters, separated by spaces.

Output format

n   A floating-point number that simulates the height of everyone from left to right in the eyes of the photographer after standing in line. Each floating-point number should be kept after the decimal point  

two   Bits, separated by a single space between two adjacent numbers.

Sample Input

6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56

Sample Output

1.65 1.72 1.78 1.70 1.61 1.56
#include<stdio.h>
#include<string.h>
struct man
{
    char sex[10];
    float hign;
};
int main()
{
    struct man a[50];
    float b[50],c[50];
    int n,e=0,f=0;
    scanf("%d",&n);
    float t=0;
    int i=0,r=0;
    for(i=0;i<n;i++)
    {
        scanf("%s %f",&a[i].sex,&a[i].hign);
        if(a[i].sex[0]=='m')
        {
                b[e]=a[i].hign;
                e++;
        }
        else
            {
                c[f]=a[i].hign;
                f++;
            }
    }
    for(i=0;i<e-1;i++)
    {
        for(r=0;r<e-1-i;r++)
        {
            if(b[r]>b[r+1])
            {
                t=b[r];
                b[r]=b[r+1];
                b[r+1]=t;
            }
        }
    }
    for(i=0;i<e;i++)
    printf("%.2f ",b[i]);
    for(i=0;i<f-1;i++)
    {
        for(r=0;r<f-1-i;r++)
        {
            if(c[r]<c[r+1])
            {
                t=c[r];
                c[r]=c[r+1];
                c[r+1]=t;
            }
        }
    }
    for(i=0;i<f;i++)
    printf("%.2f ",c[i]);
    return 0;
}

The structure is probably mastered. Look at the pointer again tomorrow. Learn the linked list again and continue to refuel!!

Posted by chodges on Sun, 28 Nov 2021 12:12:19 -0800