刷题笔记day9
【PAT A1012】 The Best Rank (25分)
此题的坑点是,排名涉及并列名次,存在并列第一,并列第三的情况,计算排名时要特别考虑。
#include<stdio.h> #include<string.h> #include<stdlib.h> struct stu { char name[10]; char maxname; int maxsort; int c; int m; int e; int a; int cc; int mm; int ee; int aa; }s[2005]; int cmpc(const void* a, const void* b) { struct stu* d = (struct stu*)a; struct stu* c = (struct stu*)b; return (*c).c-(*d).c; } int cmpm(const void* a, const void* b) { struct stu* d = (struct stu*)a; struct stu* c =(struct stu*) b; return (*c).m-(*d).m; } int cmpe(const void* a, const void* b) { struct stu* d = (struct stu*)a; struct stu* c = (struct stu*)b; return (*c).e-(*d).e; } int cmpa(const void* a, const void* b) { struct stu* d = (struct stu*)a; struct stu* c = (struct stu*)b; return (*c).a-(*d).a; } int main() { int n,m; scanf("%d%d",&n,&m); int i; for(i=0;i<n;i++) { scanf("%s%d%d%d",s[i].name,&s[i].c,&s[i].m,&s[i].e); s[i].a = (int)(s[i].c+s[i].m+s[i].e)/3; } qsort(s,n,sizeof(struct stu),cmpe); for(i=0;i<n;i++) { if(i>0 && s[i].e == s[i-1].e) { s[i].ee = s[i-1].ee; } else s[i].ee = i+1; } qsort(s,n,sizeof(struct stu),cmpm); for(i=0;i<n;i++) { if(i>0 && s[i].m == s[i-1].m) { s[i].mm = s[i-1].mm; } else s[i].mm = i+1; } qsort(s,n,sizeof(struct stu),cmpc); for(i=0;i<n;i++) { if(i>0 && s[i].c == s[i-1].c) { s[i].cc = s[i-1].cc; } else s[i].cc = i+1; } qsort(s,n,sizeof(struct stu),cmpa); for(i=0;i<n;i++) { if(i>0 && s[i].a == s[i-1].a) { s[i].aa = s[i-1].aa; } else s[i].aa = i+1; } //emca for(i=0;i<n;i++) { s[i].maxsort = s[i].ee; s[i].maxname = 'E'; if(s[i].mm<=s[i].maxsort) { s[i].maxsort = s[i].mm; s[i].maxname = 'M'; } if(s[i].cc<=s[i].maxsort) { s[i].maxsort = s[i].cc; s[i].maxname = 'C'; } if(s[i].aa<=s[i].maxsort) { s[i].maxsort = s[i].aa; s[i].maxname = 'A'; } } while(m--) { char qname[20]; scanf("%s",qname); int find = 0; for(i=0;i<n;i++) { if(strcmp(s[i].name,qname)==0) { find = 1; printf("%d %c\n",s[i].maxsort,s[i].maxname); break; } } if(!find) { printf("N/A\n"); } } }
【PAT A1014】 Waiting in Line
此题的坑点是如果是在17:00前开始,即使在17:00后结束,也均可以进行服务,要正常输出时间。
#include<stdio.h> #include<limits.h> #include<queue> using namespace std; int peopletime[1005]; int anstime[1005]; int n,m,k,q; int sumnum=0;//已经进去的人 struct Que { queue<int> qq; int num=0; int time=0; int endtime=0; }que[1005]; int findsque() { if(sumnum<n*m) { int i; int minnum = 2000; int mini=-1; for(i=0;i<n;i++) { if(minnum>que[i].num && que[i].num<=m) { minnum = que[i].num; mini = i; } } return mini; } else { int i; int mintime = INT_MAX; int mini = -1; for(i=0;i<n;i++) { if(que[i].endtime+que[i].qq.front()<mintime) { mintime = que[i].endtime + que[i].qq.front(); mini = i; } } return mini; } } int main() { scanf("%d%d%d%d",&n,&m,&k,&q); int i; for(i=1;i<=k;i++) { scanf("%d",&peopletime[i]); } for(i=1;i<=k;i++) { int quei = findsque(); //printf("quei:%d\n",quei); que[quei].qq.push(peopletime[i]); que[quei].time += peopletime[i]; anstime[i] = que[quei].time; que[quei].num++; if(sumnum>=n*m) { que[quei].endtime += que[quei].qq.front(); que[quei].qq.pop(); que[quei].num--; sumnum--; } sumnum++; } while(q--) { int quk; scanf("%d",&quk); if(anstime[quk]-peopletime[quk]>=(17-8)*60) printf("Sorry\n"); else printf("%02d:%02d\n",anstime[quk]/60+8,anstime[quk]%60); } }
【PAT A1015】Reversible Primes
‘1’不是质数。第一个测试点对此坑进行了考察。
#include<stdio.h> #include<string.h> const int maxn = 100005; int isp[maxn]={0}; int num; int rnum; int d; int n; char str[100]; void isprime() { isp[1] = 1; int i,j; for(i=2;i<maxn;i++) { if(isp[i]==0) { for(j=i+i;j<maxn;j+=i) isp[j] = 1; } } } int len; int rtrans() { int ans = 0; int i; for(i=0;i<len;i++) { ans *= d; ans += (str[i]-'0'); } return ans; } int main() { isprime(); while(scanf("%d",&n)!=EOF) { len=0; if(n<0)break; scanf("%d",&d); num = n; while(n>0) { str[len++] = (n%d+'0'); n /= d; } rnum = rtrans(); //printf("%d %d\n",num,rnum); if((!isp[num])&&(!isp[rnum])) printf("Yes\n"); else printf("No\n"); } }
7-4 是否同一棵二叉搜索树
#include<stdio.h> #include<stdlib.h> int n,l; struct node { struct node* left; struct node* right; int data; int flag; }; struct node* maketree() { int x; scanf("%d",&x); struct node* newnode = (struct node*)malloc(sizeof(struct node)); struct node* root = newnode; root->data = x; root->left = NULL; root->right = NULL; root->flag = 0; int i; for(i=1;i<n;i++) { int dx; scanf("%d",&dx); struct node* newnode = (struct node*)malloc(sizeof(struct node)); newnode->data = dx; newnode->left = newnode->right = NULL; newnode->flag = 0; struct node* tmp = root; struct node* pre; int direct = 0;//-1 1 while(tmp) { pre = tmp; if(dx>tmp->data) { tmp = tmp->right; direct = 1; } else if(dx<tmp->data) { tmp = tmp->left; direct = -1; } } if(direct == -1) { pre->left = newnode; } else if(direct == 1) { pre->right = newnode; } } return root; } void printpost(struct node* root) { if(root == NULL)return; printpost(root->left); printf("%d:%d",root->data,root->flag); if(root->left)printf(" left:%d",root->left->data); if(root->right)printf(" right:%d",root->right->data); printpost(root->right); } void clearflag(struct node* root) { if(root == NULL)return; root->flag = 0; clearflag(root->left); clearflag(root->right); } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; scanf("%d",&l); struct node* root = maketree(); //printpost(root); while(l--) { int same = 1; int i; for(i=0;i<n;i++) { int x; scanf("%d",&x); struct node* tmp = root; //printf("i:%d\n",i); //printpost(root); //printf("\n"); //printf("%d",root->left->left->data); while(tmp) { //printf("tmp->Data:%d\n",tmp->data); if(tmp->data==x) { //printf("1"); tmp->flag = 1; break; } else { if(tmp->flag == 0) { //printf("2"); same = 0; break; } else { if(tmp->data>x) { if(tmp->left == NULL) { //printf("3"); same = 0; break; } else { //printf("4"); tmp = tmp->left; } } else { if(tmp->right == NULL) { //printf("5"); same = 0; break; } else { //printf("6"); tmp = tmp->right; } } } } } } clearflag(root); if(same) printf("Yes\n"); else printf("No\n"); } } }
翻煎饼
#include<stdio.h> int a[1005]; void swap(int i,int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } void qujian(int i,int j) { while(i<j) { swap(i,j); i++; j--; } } int main() { int n; scanf("%d",&n); int i; for(i=0;i<n;i++) scanf("%d",&a[i]); int ccount = 0; int maxa = -1; int k=-1; for(i=0;i<n;i++) { int last = n-1-i; int j; maxa = -1; for(j=0;j<n-i;j++) { if(a[j]>maxa) { maxa = a[j]; k = j; } } if(k == last) { continue; } else if(k == 0) { qujian(0,last); ccount++; } else { qujian(0,k); qujian(0,last); ccount += 2; } } printf("%d\n",ccount); //for(i=0;i<n;i++) //printf("%d",a[i]); }