杭电ACM1236——排名

这一题,比较麻烦,可以说是简单的结构体的应用。

输入的数据量比较大,用scanf比较好一点,还有一点比较难的是如果分数一样,要按考生号的升序来输出。

我用一个结构体来存每一个考生的考号和总成绩,然后排序,算出有几个合格,再输出。

下面的是一次AC的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

class data
{
public:
	char str[25];                    //考号
	int sum;                         //总成绩
};
data student[1005];

int cmp(const data& a, const data& b)   //排序从大到小
{
	if(a.sum != b.sum)                  //分数不一样
		return a.sum > b.sum;
	else                                //分数一样的,比较麻烦
	{
		int x = 0, y = 0;
		int len1 = strlen(a.str);
		int len2 = strlen(b.str);
		int i = 0, j = 0;
		while((a.str[i] >= 'A' && a.str[i] <= 'Z') || (a.str[i] >= 'a' && a.str[i] <= 'z'))  //忽略前面的字母
			i++;
		while(a.str[i] != '\0')         //算a的考号后面的数字
		{
			x += x * 10 + (a.str[i] - '0');
			i++;
		}
		while((b.str[j] >= 'A' && b.str[j] <= 'Z') || (b.str[j] >= 'a' && b.str[j] <= 'z'))   //忽略前面的字母
			j++;
		while(b.str[j] != '\0')         //算b的考号后面的数字
		{
			y += y * 10 + (b.str[j] - '0');
			j++;
		}
		return x < y;              //从小到大排
	}
}

int main()
{
//	freopen("data.txt", "r", stdin);
	int N, M, G;
	int sroce[12], i, j, k;
	while(scanf("%d", &N) != EOF && N != 0)   //输入
	{
		scanf("%d%d", &M, &G);
		for(i = 1; i <= M; i++)               //每一题的分数
			scanf("%d", &sroce[i]);
		for(i = 0; i < N; i++)                
		{
			scanf("%s%d", student[i].str, &j);  //输入考号和做对几题
			student[i].sum = 0;
			while(j--)
			{
				scanf("%d", &k);
				student[i].sum += sroce[k];     //将分数加起来
			}
		}
		sort(student, student + N, cmp);        //排序
		int count = 0;
		for(i = 0; i < N; i++)                  //算合格的几个
		{
			if(student[i].sum >= G)
				count++;
			else
				break;
		}
		printf("%d\n", count);                  //输出
		for(i = 0; i < count; i++)
			printf("%s %d\n", student[i].str, student[i].sum);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值