NEW mickey 99NEW mini 88MAXAVERAGEDELETE winnieQUITSample OutputA new recordA new record99 1mickey93.50no 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;}
新闻热点
疑难解答