杭电1236

来源:互联网 发布:国际阿里云购买方法 编辑:程序博客网 时间:2024/06/14 04:30

排名

Time Limit: 1000 ms /Memory Limit: 32768 kb

Description

今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的 考生,并将他们的成绩按降序打印。

Input

测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一 名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号 (题目号由1到M)。 当读入的考生人数为0时,输入结束,该场考试不予处理。

Output

对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高 到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考 号的升序输出。

Sample Input

4 5 2510 10 12 13 15CS004 3 5 1 3CS003 5 2 4 1 3 5CS002 2 1 2CS001 3 2 3 51 2 4010 30CS001 1 22 3 2010 10 10CS000000000000000001 0CS000000000000000002 2 1 20

Sample Output

3CS003 60CS001 37CS004 3701CS000000000000000002 20
Hint
Huge input, scanf is recommended.

网上答案:
#include "iostream"    #include "stdio.h"    #include "algorithm"    #include <string.h>    using namespace std;    const int MAXN = 11;    const int MAX = 22;    int point[MAXN];    struct Student    {           char num[MAX];           int pro_num[MAXN];           int pnt ;    };    bool cmp(const Student &a,const Student &b)    {         if (a.pnt == b.pnt)         return strcmp(a.num,b.num) < 0 ? 1 : 0;         else return a.pnt > b.pnt;    }    int main()    {        int N, sum;        while (scanf("%d",&N) != EOF)        {              sum = 0;              if (N == 0) break;              int M, G;              scanf("%d%d",&M,&G);              int i;              int max;              for(i = 0; i < M; i++)              {                    scanf("%d",&point[i]);              }              Student stu[1001];              for(i = 0; i < N; i ++)              {                    scanf("%s",&stu[i].num);                    stu[i].pnt = 0;                    int slv, j;                    scanf("%d",&slv);                    for (j = 0; j < slv; j++)                    {                        scanf("%d",&stu[i].pro_num[j]);                        stu[i].pnt = stu[i].pnt + point[stu[i].pro_num[j] - 1];                    }              }              for (i = 0; i < N; i++)              {                  if (stu[i].pnt >= G) sum++;              }              printf("%d\n",sum);              sort (stu,stu + N,cmp);              for (i = 0; i < sum; i++)              {                  printf("%s %d\n", stu[i].num,stu[i].pnt);              }        }        system ("pause");        return 0;    }
开始的时候自己用快排,输出错误,模仿答案结果wa……;先mark
#include <stdio.h>#include<stdlib.h>#include <string.h>#include<algorithm>using namespace std; int N,M,G,i,j; int g[20];typedef struct node{ char num[25]; int allg;}node;void creatL(node* &L){ int t1,t2; for(i=0;i<N;i++) { scanf("%s %d",&L[i].num,&t1); for(j=0;j<t1;j++) { scanf("%d",&t2); L[i].allg+=g[t2]; } }}bool cmp(node &a,node &b){ if(a.allg==b.allg) return strcmp(a.num,b.num)<0?1:0; else return a.allg>b.allg;}int main(){ while(scanf("%d",&N)!=EOF) { if(N==0) break; int sum=0; scanf("%d %d",&M,&G); for(i=1;i<=M;i++) //学生完成的题号输入是从1开始的,从1开始记录每道题的成绩 { scanf("%d",&g[i]); } node* L=(node *)malloc(sizeof(node )*1010); creatL(L); for(i=0;i<N;i++) { if(L[i].allg>=G) sum++; } printf("%d\n",sum); sort(L,L+N,cmp); // for(i=0;i<sum;i++) { printf("%s %d\n",L[i].num,L[i].allg); }} return 0;}

1.sort()函数
c++、java里对数组的元素进行排序的方法,
使用时包含头文件algorith;使用using namespace std;
返回对数组的引用;
参数:
无参数:
当按字母顺序对数组元素进行排序时,按有小到大顺序排序
sort(a,a+L);
数组a[100],L是小于数组a元素个数(100)的数,对从a[0]到a[L-1]按升序排序
sort(sortby);
sortby:比较函数,为排序提供比较方法。
有两个参数a,b
若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
若 a 等于 b,则返回 0。 (此时不排序)
若 a 大于 b,则返回一个大于 0 的值。
本题中利用sort()函数对结构体进行排序,提供比较函数cmp(),



0 0