题目:PAT 甲级 1062
为什么我用 字符数组类型的 考号是输入会错啊
以下是用int类型的代码 可AC
#include <cstdio>
#include <algorithm>
#include<cstring>
using namespace std;
/*
*******************************************************************************
*********疑问:为什么Examinee的id项改成char id[10]之后会出错??*******************
**********************输入部分哪里错了?求指教***********************************
*******************************************************************************
*/struct Examinee
{//char id[10];int id;//考号int moral_score;//德分int capacity_score;//才分int examinee_rank;//考生等级};bool cmp(Examinee a, Examinee b)//sort()的比较函数
{if(a.examinee_rank!=b.examinee_rank) return a.examinee_rank<b.examinee_rank;else if((a.capacity_score+a.moral_score)!=(b.capacity_score+b.moral_score)) return (a.capacity_score+a.moral_score)>(b.capacity_score+b.moral_score);else if(a.moral_score!=b.moral_score) return a.moral_score > b.moral_score;//else return strcmp(a.id,b.id)<0;else return a.id<b.id;
}int judgeRank(int moral_score,int capacity_score,int L,int H)//判断考生的等级
{if(moral_score<L ||capacity_score<L)return 5;if(moral_score>= H && capacity_score>=H)return 1;else if(moral_score>=H &&capacity_score<H&&capacity_score>=L)return 2;else if(moral_score<H &&capacity_score<H&&moral_score>=L&&capacity_score>=L&&moral_score>=capacity_score)return 3;elsereturn 4;}int main()
{Examinee examinee[100010];memset(examinee,0,sizeof(struct Examinee)*100010);int N,L,H;scanf("%d %d %d",&N,&L,&H);int num_of_passed=N;for(int i = 0;i<N;i++){//scanf("%s %d %d",examinee[i].id,&examinee[i].moral_score,&examinee[i].capacity_score);scanf("%d %d %d",&examinee[i].id,&examinee[i].moral_score,&examinee[i].capacity_score);examinee[i].examinee_rank = judgeRank(examinee[i].moral_score,examinee[i].capacity_score,L,H);if(examinee[i].examinee_rank == 5) num_of_passed--;}sort(examinee,examinee+N,cmp);printf("%d\n",num_of_passed);for(int i = 0 ;i<N;i++){if(examinee[i].examinee_rank==5) break;//printf("%s %d %d\n",examinee[i].id,examinee[i].moral_score,examinee[i].capacity_score);printf("%d %d %d\n",examinee[i].id,examinee[i].moral_score,examinee[i].capacity_score);}return 0;
}