首页 > ACM题库 > HDU-杭电 > hdu 2142 disney-模拟-[解题报告]C++
2013
12-29

hdu 2142 disney-模拟-[解题报告]C++

disney

问题描述 :

  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.

输入:

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)

输出:

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)

样例输入:

NEW mickey 99
NEW mini 88
MAX
AVERAGE
DELETE winnie
QUIT

样例输出:

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

题目:hdu 2142 disney

思路:用STL里的map直接模拟

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
map<string,int>m;
int main()
{
    m.clear();
    map<string,int>::iterator it,se;
    string s;
    string str;
    int grade;
    double sum=0;
    while(cin>>s)
    {
        if(s=="QUIT")
            break;
        if(s=="NEW")
        {
            cin>>str;
            cin>>grade;
            se=m.find(str);
            if(se==m.end())
                printf("A new record\n");
            else
            {
                sum-=m[str];
                printf("update succeed\n");
            }
            m[str]=grade;
            sum+=grade;
        }
        else if(s=="MAX")
        {
            vector<string>v;
            v.clear();
            int mx=m.begin()->second;
            for(it=m.begin();it!=m.end();it++)
                mx=max(mx,it->second);
            for(it=m.begin();it!=m.end();it++)
            {
                if(it->second==mx)
                {
                    v.push_back(it->first);
                }
            }
            printf("%d %d\n",mx,v.size());
            sort(v.begin(),v.end());
            vector<string>::iterator ii=v.begin();
            for(;ii!=v.end();ii++)
                cout<<*ii<<endl;
        }
        else if(s=="AVERAGE")
        {
            printf("%.2lf\n",sum/m.size());
        }
        else if(s=="DELETE")
        {
            cin>>str;
            se=m.find(str);
            if(se==m.end())
                printf("no such record\n");
            else
            {
                sum-=se->second;
                m.erase(se);
                printf("delete succeed\n");
            }
        }
    }
    return 0;
}

解题转自:http://blog.csdn.net/shiyuankongbu/article/details/8859939


  1. 第一题是不是可以这样想,生了n孩子的家庭等价于n个家庭各生了一个1个孩子,这样最后男女的比例还是1:1