zoukankan      html  css  js  c++  java
  • POJ 2153 Rank List (map映射)

    水题,竟然花了那么多时间。。。主要是不知道为什么,明明在本机上编译过去了,但是用c++提交却编译错误。。。最后用g++提交AC


    题意:给出n个学生的名字,然后给出m个测验。
      每个测验给出n个学生的分数。
      当给出第i次测验的成绩,求Li Ming在所有学生中,前i次成绩总和的排名(若分数相同,则Li Ming排在第一个)

      开始没仔细看题,以为对于每次测验,只要给出Li Ming在此次测验中的排名。
      后来才知道,原来题目中有这么一句话:
      In the i-th line, you should give the rank of Li Ming after the i-th exam.
      The rank is decided by the total scores。。。

    思路:用map建立映射关系,最后求名次的时候for循环一遍即可。

    #include <iostream>
    #include <stdio.h>
    #include <map>
    #include <string.h>
    #include <algorithm>
    
    const int maxn=10010;
    using namespace std;
    int n,m;
    int score[maxn];
    map<string,int> stu;  //建立学生名字到编号的映射
    
    int main()
    {
        char str[50];
        int fenshu;
        cin>>n;
        getchar();
        for(int i=1;i<=n;i++){
            gets(str);  //用gets读取名字
            stu[str]=i;
        }
        cin>>m;
        memset(score,0,sizeof(score));
        while(m--){
            for(int i=1;i<=n;i++){
                scanf("%d",&fenshu);
                getchar(); //先读取分数后面的一个空格
                gets(str);
                score[stu[str]]+=fenshu;
            }
            int ranks=1;  //李明的排名
            for(int i=1;i<=n;i++)
                if(score[i]>score[stu["Li Ming"]])
                    ranks++;
            cout<<ranks<<endl;
        }
        return 0;
    }
  • 相关阅读:
    openCR-用ROS代码获取电压的方法
    openCR-用ROS代码控制按键的方法
    TurtleBot3-基础例程
    TurtleBot3-键盘远程控制
    TurtleBot3-Topic Monitor
    openCR-串口打印HelloWorld
    Ubuntu播放音乐 mp3 wav
    SQL Server2008创建约束图解 转
    height、clientHeight、scrollHeight、offsetHeight区别
    第一次弄项目有感
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/3525346.html
Copyright © 2011-2022 走看看