hdu 1242 disney (map模拟)

5 篇文章 0 订阅


disney

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1039    Accepted Submission(s): 299


Problem Description
  A new machine was introduced into disney world. Every day, there is a long queue because of its function. Everyone can input their score or update their score. If you want to know the maximum or the average, it can help you, too.What's more, if you don't want that your score be saw, you can delete your register from it. Since the new machine was introduced, everyone be more hard.
 

Input
The input contain only one case.
There are kinds of format that show different functions:
(1)NEW name score(you can use it to input your score or update your score)
name: the name of current user.The length of the name is no longer than 8.
score: the score of current user.(0<score<=100)
(2)AVERAGE(you can use is to get the average score)
(3)MAX(you can use is to get the maximal score and whose score is the maximum.)
(4)DELETE name: delete the name and its record too.
name: the name of current user.The length of the name is no longer than 8.
(5)QUIT(this command is used to turn off the machine,is the end of the input)
 

Output
(1)For "NEW name score" , if it's there is a lack of record of the user. print "A new record".else print "update succeed".
(2)For "AVERAGE" , you should print the average score rounded to two digits after the decimal point.if there are none record in the machine,the average is 0.00;
(3)For "MAX" , you should first print the maximal score and the number that whose score is the maximum, then print all of their names Lexicographicly, and each name owns a single line. If there isn't any record in the machine,just output"0 0".
(4)For "DELETE name" , if there is a lack of record of the user. print "no such record".else print "delete succeed".

The population of disney is no more than 100.
 

Sample Input
      
      
NEW mickey 99 NEW mini 88 MAX AVERAGE DELETE winnie QUIT
 

Sample Output
      
      
A new record A new record 99 1 mickey 93.50 no such record
 

Author
kiki
 

Source


思路:用map来模拟。

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

int main()
{
    string n;
    map<string,int> m;
    map<string,int> ::iterator it;
    double sum=0;
    while(cin>>n)
    {
        if(n=="NEW")
        {
            string name;
            int grade;
            cin>>name;
            cin>>grade;
            it=m.find(name);
            if(it==m.end())
            {
                m[name]=grade;
                printf("A new record\n");
                sum+=grade;
            }
            else
            {
                printf("update succeed\n");
                sum-=m[name];
                sum+=grade;
                m[name]=grade;
            }
        }
        else if(n=="AVERAGE")
        {
            if(m.size()==0)
            {
                printf("0.00\n");
            }
            else
            {
                double ave=sum/(m.size()*1.0);
                printf("%.2lf\n",ave);
            }
        }
        else if(n=="MAX")
        {
            if(m.size()==0)
            {
                printf("0 0\n");
            }
            else
            {
                int maxn=m.begin()->second;
                for(it=m.begin();it!=m.end();it++)
                {
                    if(it->second>maxn) maxn=it->second;
                }
                vector<string> f;
                f.clear();
                for(it=m.begin();it!=m.end();it++)
                {
                    if(it->second==maxn)
                    {
                        f.push_back(it->first);
                    }
                }
                sort(f.begin(),f.end());
                printf("%d %d\n",maxn,f.size());
                for(int i=0;i<f.size();i++)
                {
                    cout<<f[i]<<endl;
                }
            }
        }
        else if(n=="DELETE")
        {
            string nam;
            cin>>nam;
            it=m.find(nam);
            if(it==m.end())
            {
                printf("no such record\n");
            }
            else
            {
                printf("delete succeed\n");
                sum-=it->second;
                m.erase(it);
            }
        }
        else break;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值