超级水题,map乱搞,建议不要看我代码,看不懂~
队里的学弟wa惨了,叫我写,约定wa三次内请我吃饭,然后wa了一发就a了~
这个题目,就是求一个最大值,输出这个人的名字就好
但是如果最大值是多大,就输出最先至少达到最大值的人(这是个坑点,因为他可能有负数,所以就是我们先判断是不是大于max,还要看这个人最后的结果是不是最大值)
多加几个判断和循环~
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
map<string, int>num;
map<string, int>num_se;
map<string, int>vis;
map<string, int>num1;
map<int, string>num2;
struct data{
string s;
int score;
}g[1005];
int main()
{
int n, i;
scanf("%d", &n);
string s;
string first;
int score;
int k = 0;
for(i = 1; i <= n; i++)
{
cin >> s >> score;
num[s] += score;
g[i].s = s;
g[i].score = score;
if(vis[s] == 0) //名字没有出现过
{
vis[s] = 1;
num1[s] = k;
num2[k] = s;
k++;
}
}
int maxx = -1000000;
int flag = 0;
string name;
int sum = 0;
for(i = 0; i < k; i++)
{
if(num[num2[i]] > maxx)
{
name = num2[i];
maxx = num[num2[i]];
}
}
for(i = 0; i < k; i++)
if(maxx == num[num2[i]])
sum++;
if(sum > 1)
flag = 1;
//printf("%d\n", maxx);
//printf("%d\n", flag);
//for(i = 0; i < k; i++)
//cout << num2[i] << " "<< num[num2[i]] << endl;
int flag1 = 0;
for(i = 1; i <= n; i++)
{
num_se[g[i].s] += g[i].score;
if(num_se[g[i].s] >= maxx && flag1 == 0)
{
if(num[num2[num1[g[i].s]]] == maxx)
{
first = g[i].s;
flag1 = 1;
//cout << first << endl;
}
}
}
if(flag == 1)
cout << first << endl;
else if(flag == 0)
cout << name << endl;
}