Hang Dian's 17 year written test

Posted by paulb on Tue, 31 Mar 2020 20:34:08 +0200

Catalog

Topic 1

Guan Yu cuts three generals and inputs the force value of four people (greater than 0 and less than 50). If the force value exceeds the limit, it needs to be input again. If the force value of Guan Yu is x, the force value of generals is y, which satisfies (x-y) ^ 2+(x-y)+41, if it is a prime number, Guan Yu wins, if it wins three times, it outputs WIN, if it loses, it outputs the serial number of the failed generals (the third pass).
Train of thought:
1. Give priority to prime number typing
2. Using the result of formula to judge the value of prime number

#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 10000
using namespace std;

bool prime[MAX];
void is_prime(){
    memset(prime,true,MAX);
    for(int i=2;i<MAX;i++){
        if(prime[i]){
            for(int j=2*i;j<MAX;j+=i)
                prime[j]=false;
        }       
    }
    prime[0]=false;prime[1]=false;
}
bool solve(int x,int y){
    int temp=(x-y)*(x-y)+(x-y)+41;
    if(prime[temp])//If the result of the formula is a prime, it's about winning 
        return true;
    else
        return false;
}
int main(){
    int enemy[4],x,i;
    is_prime();//Carry out pretreatment operation, 
    while(true){
        bool flag=true;//Whether the mark input is legal 
        scanf("%d",&x);
        if(x<=0||x>=50)
            flag=false; 
        for(i=1;i<4;i++){
            scanf("%d",&enemy[i]);
            if(enemy[i]<=0||enemy[i]>=50)
                flag=false; 
        }
        if(flag)
            break;
        else{
            printf("Force value should be between 0 and 50\n");
            continue;
        }
    }
    bool flag=true;//If you enter one, mark it as false immediately; 
    for(i=1;i<4;i++){
        if(!solve(x,enemy[i])){
            flag=false;
            break;
        }   
    }
    if(flag){
        printf("WIN\n");
    }
    else
        printf("Guan Yu loses to the first%d General",i);
    return 0;
}

It does not guarantee the correctness of the answer, but provides a way of thinking

Topic two:

Input N employees, each employee output ID number, on duty time, off duty time, the first line output ID of the earliest employee and on duty time, the second line output ID of the most delayed employee and off duty time, the third line output ID and on duty time of the longest working employee (data falsified)
sampleinput:
ID100001,07:00:0017:00:00
ID100002,08:00:0018:00:00
ID100003,09:00:0021:00:00
sampleout:
OPEN:ID100001,07:00:00
CLOSE:ID100003,21:00:00
(..):ID100003,12:00:00

#include<stdio.h>
#include<string.h>
#include<time.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct time{
    int hour;
    int min;
    int sec;    
};
struct employer{
    char id[100];
    struct time t1,t2;
    int diff;
}buf[100];
bool cmp1(employer a,employer b){//Compare the earliest working hours 
    if(a.t1.hour!=b.t1.hour)
        return a.t1.hour<b.t1.hour;
    else if(a.t1.min!=b.t1.min)
        return a.t1.min<b.t1.min;
    else
        return a.t1.sec<b.t1.sec;
}
bool cmp2(employer a,employer b){//Compare the late ones 
    if(a.t2.hour!=b.t2.hour)
        return a.t2.hour>b.t2.hour;
    else if(a.t2.min!=b.t2.min)
        return a.t2.min>b.t2.min;
    else
        return a.t2.sec>b.t2.sec;
}
bool cmp3(employer a,employer b){//Compare the longest stay in the unit 
    return a.diff>b.diff; 
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s%d:%d:%d %d:%d:%d",&buf[i].id,&buf[i].t1.hour,&buf[i].t1.min,&buf[i].t1.sec,
        &buf[i].t2.hour,&buf[i].t2.min,&buf[i].t2.sec);
        buf[i].diff=(buf[i].t1.hour*3600+buf[i].t1.min*60+buf[i].t1.sec)-(buf[i].t2.hour*3600+buf[i].t2.min*60+buf[i].t2.sec);  
    }
    //Every time a comparison is made, only one output is made 
    sort(buf,buf+n,cmp1);
    printf("%s %d:%d:%d\n",buf[0].id,buf[0].t1.hour,buf[0].t1.min,buf[0].t1.sec);
    sort(buf,buf+n,cmp2);
    printf("%s %d:%d:%d\n",buf[0].id,buf[0].t2.hour,buf[0].t2.min,buf[0].t2.sec);
    sort(buf,buf+n,cmp3);
    printf("%s %d:%d:%d\n",buf[0].id,buf[0].t1.hour,buf[0].t1.min,buf[0].t1.sec);
    return 0;
}

Topic three:

There is a M*N material and a s*t template. Cut the template from the material to find the maximum number of templates that can be cut out.
sample input
:
3 4
a b c d
c d a b
a c c d
2 2
a b
c d
sample out
2
sample output
:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define MX 100
#define MY 100
char map_a[MX][MY];//Storage map
char map_b[MX][MY];//Storage map
//char temp[MX][MY]; / / store the changed graph 
using namespace std;
int m,n,s,t,ans;//Used to store the length and width of large and small graphs 
bool judge(char Map[MX][MY],int x,int y){//Determine whether the big picture contains the small picture 
    for(int i=0;i<s;i++){
        for(int j=0;j<t;j++)
            if(Map[x+i][y+j]!=map_b[i][j])
                return false;
    } 
    return true;
}
void dfs(char Map[MX][MY],int x,int y,int max){
    if(max>ans)//Update answers 
        ans=max;
    if(y>=n)//Give program exit 
        return ;
    if(x>=m)
        dfs(Map,0,y+1,max);//After the first train is finished, change to the second one 
    else{
        if(judge(Map,x,y)){
            char temp[MX][MY]={0};
            for(int i=0;i<m;i++)//Initialize middle graph 
                for(int j=0;j<n;j++)
                    temp[i][j]=Map[i][j];
            for(int i=0;i<s;i++)
                for(int j=0;j<t;j++)
                    temp[x+i][y+j]='0';//Mark the matching places as 0 
            dfs(temp,x+s,y,max+1);//Make sure to take this one 
            dfs(Map,x+1,y,max);//If you don't want to join us, keep going step by step 
        }
        else
            dfs(Map,x+1,y,max);//Column if not matched        
    } 

}
int main(){
    cin>>m>>n;//Enter the length and width of the large drawing 
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            cin>>map_a[i][j]; 
    cin>>s>>t;//Enter the length and width of a small graph 
    for(int i=0;i<s;i++)
        for(int j=0;j<t;j++)
            cin>>map_b[i][j];
    ans=0;
    dfs(map_a,0,0,0); 
    cout<<ans<<endl;    
    return 0;
} 

Topics: less