首页 > 学院 > 开发设计 > 正文

disney (map模拟)

2019-11-06 06:18:47
字体:
来源:转载
供稿:网友
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. InputThe 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 winnieQUITSample Output
A new recordA new record99 1mickey93.50

no such record

#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <algorithm>#include <vector>#include <string>#include <cstring>#include <sstream>#define INF 100000000using namespace std;map<string,int> M;double sum;int main(){    string s;    map<string,int>::iterator i;    while(cin>>s)    {        if(s=="QUIT")        {            return 0;        }        else if(s=="NEW")        {            string name;            cin>>name;            int grade;            cin>>grade;            i=M.find(name);            if(i==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(s=="AVERAGE")        {            if(M.size()==0)            {                printf("0.00/n");            }            else            {                double ave=sum*1.0/M.size();                printf("%.2f/n",ave);            }        }        else if(s=="MAX")        {            if(M.size()==0)            {                printf("0 0/n");            }            else            {                map<string,int>::iterator it;                int maxx=M.begin()->second;                for(it=M.begin();it!=M.end();it++)                {                    if(it->second>maxx) maxx=it->second;                }                vector<string> b;                b.clear();                for(it=M.begin();it!=M.end();it++)                {                    if(it->second==maxx)                    {                        b.push_back(it->first);                    }                }                sort(b.begin(),b.end());                printf("%d %d/n",maxx,b.size());                for(int i=0;i<b.size();i++)                {                    cout<<b[i]<<endl;                }            }        }        else if(s=="DELETE")        {            string name;            cin>>name;            i=M.find(name);            if(i==M.end())            {                printf("no such record/n");            }            else            {                printf("delete succeed/n");                sum-=M[name];                M[name]=0;                map<string,int>::iterator it;                M.erase(i);            }        }    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表