hdu 1242 disney (map模拟)

来源:互联网 发布:阿尔法营销软件 编辑:程序博客网 时间:2024/06/08 17:51


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 99NEW mini 88MAXAVERAGEDELETE winnieQUIT
 

Sample Output
A new recordA new record99 1mickey93.50no such record
 

Author
kiki
 

Source
HDU 2007-11 Programming Contest 


思路:用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;}