PAT甲级(1011-1015)

1011

https://pintia.cn/problem-sets/994805342720868352/problems/994805504927186944
在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iomanip>
#include <string>

using namespace std;

struct ra{
    double ratee;
    int index;
};

double rate[3];
ra tmp[3];
int ind[3];

bool cmp(ra r1,ra r2){
    return r1.ratee>r2.ratee;
}

char turn(int x){
    if (x == 0){return 'W';}
    else if (x == 1){return 'T';}
    else {return 'L';}
}

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    for (int i=0;i<3;i++){
        for (int j=0;j<3;j++){
            cin>>tmp[j].ratee;
            tmp[j].index = j;
        }
        sort(tmp,tmp+3,cmp);
        rate[i] = tmp[0].ratee;
        ind[i] = tmp[0].index;
    }
    double result = 1;
    for (int i=0;i<3;i++){
        result *= rate[i];
        cout<<turn(ind[i])<<' ';
    }
    result = (result*0.65-1)*2;
    cout<<fixed << setprecision(2)<<result;
}

1012

https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480
在这里插入图片描述
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

int m,n;//m学生人数,n待查学生数

struct stu{
    string id;
    int c;
    int m;
    int e;
    int all;//通用属性,排序用
    int rank;
    char way;
};

const int MAXN = 100000;

stu student [MAXN];
map <string,stu> mp;

bool cmp(stu s1,stu s2){
    return s1.all>s2.all;
}

void order(stu s[],char way){
    int cou = 0;//排名
    s[0].rank = 0;
    for (int i=1;i<m;i++){
        if (s[i].all == s[i-1].all){
            s[i].rank = cou;
        }
        else {s[i].rank = i;cou = i;}
    }
    for (int i=0;i<m;i++){
        if (mp[s[i].id].rank>= s[i].rank){//排名更优秀,更新排名
            mp[s[i].id].rank = s[i].rank;
            mp[s[i].id].way = way;
        }
    }
}


int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    cin>>m>>n;
    if (m == 0 || n == 0){return 0;}
    for (int i=0;i<m;i++){
        stu stmp;
        cin>>stmp.id;
        cin>>stmp.c;
        cin>>stmp.m;
        cin>>stmp.e;
        stmp.rank = m;
        stmp.way = 'Z';
        stmp.all = 1;
        student[i] = stmp;
        mp[stmp.id] = stmp;
    }
    for (int i=0;i<m;i++){
        student[i].all = student[i].e;
    }
    sort(student,student+m,cmp);
    order(student,'E');
    for (int i=0;i<m;i++){
        student[i].all = student[i].m;
    }
    sort(student,student+m,cmp);
    order(student,'M');
    for (int i=0;i<m;i++){
        student[i].all = student[i].c;
    }
    sort(student,student+m,cmp);
    order(student,'C');
    for (int i=0;i<m;i++){
        student[i].all = student[i].e+student[i].m+student[i].c;
    }
    sort(student,student+m,cmp);
    order(student,'A');
    for (int i=0;i<n;i++){
        string id;
        cin>>id;
        if (mp.find(id)!=mp.end()){//存在学生学号
            cout<<mp[id].rank+1<<' '<<mp[id].way<<endl;
        }
        else {
            cout<<"N/A"<<endl;
        }
    }
}

1013

https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

const int MAXN = 500000;

struct edge{//边
    int x,y;
};

int father[MAXN];
int height[MAXN];
edge edg[MAXN];

void initial(int n){
    for (int i=0;i<=n;i++){
        father[i] = i;
        height[i] = 0;
    }
}

int Find(int x){
    if (x != father[x]){
        father[x] = Find(father[x]);
    }
    return father[x];
}

void Union(int x,int y){
    x = Find(x);
    y = Find(y);
    if (x!=y){
        if (height[x]<height[y]){
            father[x] = y;
        }
        else if (height[x]>height[y]){
            father[y] = x;
        }
        else {
            father[y] = x;
            height[x]++;
        }
    }
    return;
}

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int n,m,k;
    cin>>n>>m>>k;
    for (int i=0;i<m;i++){
        cin>>edg[i].x>>edg[i].y;//存储所有的边
    }
    for (int i=0;i<k;i++){
        initial(n);
        int lack;
        cin>>lack;//缺失的城市
        for (int j=0;j<m;j++){
            if (edg[j].x!=lack&&edg[j].y!=lack){//城市没被占领
                Union(edg[j].x,edg[j].y);
            }
        }
        int answer = -1;
        for (int i=1;i<=n;i++){
            if (Find(i) == i){
                answer++;
            }
        }
        cout<<answer-1<<endl;
    }

}

1014

https://pintia.cn/problem-sets/994805342720868352/problems/994805498207911936
在这里插入图片描述
在这里插入图片描述
后2个测试点通不过

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

struct customer{
    int id;
    int startime;//开始办理时间
    int endtime;//结束办理时间
    int needtime;//所需要的时间
    int window;//处于哪个窗口
};

const int MAXN = 500000;
int n,m,k,q;//n窗口数目,m每个窗口容量,k顾客,q待查顾客

struct cmp {
	bool operator()(const customer &a, const customer &b){
		if (a.endtime != b.endtime){
			return a.endtime > b.endtime;//结束时间早的先出
		}
		else{
			return a.window > b.window;//结束时间相同,窗口小的先出
		}
	}
};

priority_queue <customer,vector<customer>,cmp> que;
customer cus[MAXN];
int wintime[MAXN];//当前窗口的最后一位结束时间

int main(){
    freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    memset(wintime, 0, sizeof(wintime));
    cin>>n>>m>>k>>q;
    for (int i=1;i<=k;i++){
        cin>>cus[i].needtime;
        cus[i].id = i;
        cus[i].startime = 0;
        cus[i].window = MAXN;
    }
    //先安排前n*m个人入队
    for (int i=0;i<m;i++){
        for (int j=1;j<=n;j++){//j窗口
            cus[i*n+j].window = j;
            if (i == 0){//第一拨人
                cus[i*n+j].startime = 0;
            }
            else {
                cus[i*n+j].startime = cus[(i-1)*n+j].endtime;//同窗口上一位的结束时间是自己开始时间
            }
            cus[i*n+j].endtime = cus[i*n+j].startime+cus[i*n+j].needtime;//计算自己的结束时间
            wintime[j] = cus[i*n+j].endtime;//更新窗口结束时间
            que.push(cus[i*n+j]);
        }
    }
    int current = n*m;//当前办理人数,current+1是待办的id
    while(current<=k){
        customer tmp = que.top();//办理完毕的人
        que.pop();
        current++;
        cus[current].window = tmp.window;//空出的窗口
        cus[current].id = current;
        cus[current].startime = wintime[tmp.window];//窗口结束时间则为新来的人的开始时间
        cus[current].endtime = cus[current].startime+cus[current].needtime;//计算自己结束时间
        wintime[tmp.window] = cus[current].endtime;//更新窗口结束时间
        que.push(cus[current]);
    }
    //处理待查人员
    for (int i=2;i<=q;i++){
        int x;
        cin>>x;
        if (cus[x].startime>=540){cout<<"Sorry"<<endl;continue;}
        int hour = 8;
        while(cus[x].endtime>=60){hour++;cus[x].endtime-=60;}
        printf("\n%02d:%02d",hour,cus[x].endtime);
    }
}

1015

https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000
在这里插入图片描述

#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
#include <math.h>

using namespace std;

const

bool isprime( int num )
{
    if(num ==0|| num==1 ){
     return 0 ;
   }
   if(num ==2|| num==3 ){
     return 1 ;
   }
   if(num %6!= 1&&num %6!= 5){
     return 0 ;
   }
   double tmp =sqrt((float)num);
   for(int i= 5;i <=tmp; i+=6 ){
      if(num %i== 0||num %(i+ 2)==0 ){
         return 0 ;
      }
   }
   return 1 ;
}

string tentoradix(int x,int radix){
    string result = "";
    while(x!=0){
        result += to_string(x%radix);//int转 string
        x /= radix;
    }
    return result;
}

int radixtoten(string x,int radix){
    int result=0;
    for (int i=0;i<x.size();i++){
        result += x[i]-'0';
        result *= radix;
    }
    result /= radix;
    return result;
}

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int n;
    int radix;
    //cin>>n>>radix;
    //int tmp = stoi(tentoradix(10,10));//string转int
    while(true){
        cin>>n;
        if (n<0){break;}
        cin>>radix;
        if (radix == 0){cout<<"No"<<endl;}
        string tmp1 = tentoradix(n,radix);
        int tmp2 = radixtoten(tmp1,radix);
        if (isprime(n)&&isprime(tmp2)){cout<<"Yes"<<endl;}
        else {cout<<"No"<<endl;}
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值