zju2005排名_结构体

来源:互联网 发布:dns 端口号 编辑:程序博客网 时间:2024/06/02 05:04

http://acm.hdu.edu.cn/showproblem.php?pid=1236

浙大计算机研究生复试上机考试-2005年

不要使用标记数组,while(x--)写的很巧妙

#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;struct E{char no[30];                //准考证号    int score;                  //最终得分};E stu[1004];bool cmp(E A,E B){   if (A.score!=B.score)   {   return A.score>B.score;   }   else    return strcmp(A.no,B.no)<0;}int main(){int n,m,g,i,k,x;                   //学生数n,考题数m,分数线g,解决题目数xint mark[15];                         //保存题目对应分值1~10道题目while (scanf("%d",&n)&& n!=0){scanf("%d%d",&m,&g);             //题目总数,分数线for (i=1;i<=m;i++){scanf("%d",&mark[i]);         //依次输入m道题目对应的分值}for(i=1;i<=n;i++)                 //n个学生成绩具体信息录入 {int s=0;                      //得分scanf("%s%d",stu[i].no,&x);   //编号no,做出题目数xwhile (x--)                   //做出x道,累加x次分数,巧妙地写法{scanf("%d",&k);                s+=mark[k];}stu[i].score=s;                //保存在stu[i].score中}sort(stu+1,stu+1+n,cmp);           //学生1~n排序int ans=0;                         //ans保存过线人数for (i=1;i<=n;i++){if (stu[i].score>=g){ans++;}}printf("%d\n",ans);for (i=1;i<=n;i++){if (stu[i].score>=g){printf("%s %d\n",stu[i].no,stu[i].score);}}}return 0;}