Hangzhou Dianzi University & East China Normal University computer topic

The first problem: Guan Yu enters a pass

Requirements: Guan Yu passes 3 generals, guan Yu’s force value is X, the enemy’s force value is Y, if it meets (X-Y) ^2+ X-Y +41 if it is prime, Guan Yu wins, if it wins all 3 times, the loser outputs the failed pass number.

Input format:

Guan Yu is x general 1y general 2y General 3y

Output format:

WIN or failed close number

Ideas:

Write a prime discriminator function and a force value comparison function, note the header file math.h (with open root function)

#include <stdio.h> #include <math.h> int wulizhi(int x,int y){ int num=x-y; return num*num+num+41; } bool isprime(int num){if(num<=3){return num>1; }// If (num%6! =1&&num&&6! =5){ return false; } for(int i=5; i<=sqrt(num); i+=6){ if(num%i=0||(num)%(i+2)==0){ return false; } } return true; } int main(){ int guan,em[4],lose=0; scanf("%d",&guan); for(int i=1; i<=3; i++){ scanf("%d \n",em[i];) } for(int i=1; i<=3; i++){ if(isprime(wulizhi(em[i]))){ continue; } else { lose=i; printf("lost to the Np.%d enemy",lose); } } if(lose==0) printf("WIN"); Return 0; }Copy the code

Second problem: capitalist simulator

Requirements: Input N employees, each employee ID number, working time, off time, the first line output the ID of the earliest employee to the company, the second line output the ID of the latest employee, the last line output the ID of the longest employee.

Input format:

Enter N in the first line to indicate that there are N employees. In the next N lines, enter the IDS of employees, working time, and off time.

Output format:

3 employees who output the above requirements

Ideas:

Create a structure that contains information about employees. Then write 3 different CMPS and sort them using 3 sort functions.

#include <stdio.h> struct person{ char id; int ts=0,te=0; int worktime; }people[1001]; Bool CMPS (person a,person b){return a.ts<b.ts; } bool cmpe(person a,person b){return a.te>b.te; } bool CMPL (person a,person b){return a.log time> b.log time; } int main(){ int N; scanf("%d",&N); for(int i=0; i<N; i++){ int tsh,tsm,tss,teh,tem,tes; scanf("%c",&people[i+1].ID); scanf("%d:%d:%d %d:%d:%d",&tsh,&tsm,&tss,&teh,&tem,&tes); people[i+1].ts=tsh*3600+tsm*60+tss; people[i+1].te=tsh*3600+tsm*60+tss; people[i+1].worktime=people[i+1].te-people[i+1].ts; } sort(people[1].tsh,people[N].tsh,cmps); printf("%c\n",people[1].id); sort(people[1].teh,people[N].teh,cmpe); printf("%c\n",people[1].id); sort(people[1].worktime,people[N].worktime,cmpl); printf("%c\n",people[N].id); return 0; }Copy the code

Problem three: arithmetics

Requirements: primary school students will calculate 1 digit and 1 digit operation of the algebra.

Input format:

Legitimate expression

Output format:

The evaluated value

Ideas:

The switch function identifies different operators

#include <stdio.h> #include <math.h> int main(){ int num1,num2; char ch; while(scanf("%d %c %d",&num1,&ch,&num2)! =EOF){ switch(ch){ case'+': printf("%d",num1+num2); break; case'-': printf("%d",num1-num2); break case'*': printf("%d",num1*num2); break case'/': printf("%d",num1/num2); break } } return 0; }Copy the code

Question 4: Group size

Requirements: the members of a group only know who their leader is, and the members under the leadership of the same group do not know each other. Now the team wants a program that counts the size of the group led by each leader, and how many people are in the group led by each leader.

Input format:

The first row is N, the number of people in the group and the second row is N numbers, and each number is the number of people in the group and the group leader. The leader of the team is represented by 0, indicating that no one is the leader of the team and the data is guaranteed to have no loop. A single member is considered a group of one.

Output format:

Row 1 contains N digits u, representing the size of the ith member

Ideas:

First find the parent node, and then print it in turn

#include <stdio.h> #include <math.h> int main(){ int N; scanf("%d",&N); int count[300],a[300]; for(int i=1; i<=N; i++){ scanf("%d ",&a[i]); } for(int i=1; i<N; i++){ if(a[i]==0){ continue; } else{ int k=i; While (a[k]! Int j=a[k]; int j=a[k]; count[j]++; k=j; } } } for(int i=1; i<N; i++){ printf("%d",count[i]); } printf("%d",count[N]); return 0; }Copy the code

bibliography

1. Computer software related professional retest guidance ———— PI PI Grey

2. Guidance for the 2022 Postgraduate Entrance examination on Data Structure ———— Wang Dao Forum