HDUOJ 1236 排名

这道题花了挺多时间,主要因为以下问题

一、字符串的输入,这道题里用scanf更为安全

二、每组数据结束后结构体内容应该清空,否则影响下组数据

三、由于我的代码里成绩的读取是从1开始的,所以循环时要注意(因为解题数总为正数)

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct node
{
    char name[100];
    int score = 0;
} stu[1024];

bool cmp(node a, node b)
{
    if (a.score != b.score)
        return a.score>b.score;
    return strcmp(a.name, b.name)>0 ? 0 : 1;
}

int main()
{
    int n, m, g = 0;
    //输入参数
    while (cin >> n >> m >> g)
    {
        int arr[100]={0};
        int pass = 0;
        //动态分配后导入分
        for (int i = 1; i <= m; i++)
        {
            cin >> arr[i];
        }
        //开始导入学生信息
        for (int i = 0; i < n; i++)
        {
            int t = 0;
            //姓名
            scanf("%s", &stu[i].name);
            //解题数
            scanf("%d", &t);
            //统计分数
            while (t!=0)
            {
                int temp = 0;
                scanf("%d", &temp);
                stu[i].score += arr[temp];
                t--;
            }
        }
        //对学生排序
        sort(stu, stu + n, cmp);
        //统计及格人数
        for (int i = 0; i < n; i++)
        {
            if (stu[i].score >= g)
                pass++;
        }
        cout << pass << endl;
        //输出学生信息
        for (int i = 0; i < pass; i++)
        {
            printf("%s %d\n", stu[i].name, stu[i].score);
        }
        for(int i=0;i<n;i++){
            memset(stu[i].name,0,100);
            stu[i].score=0;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/DaiShuSs/p/9643225.html