求助,为啥第一个点就错了。 求大佬指点。
#include <bits/stdc++.h>
using namespace std ;
struct User
{
string name ;
int score ;
} ;
int head ;
string a[1050] ;
int b[1050] ;
User u[1050] ;
int n ;
int find(string k)
{
for (int i = 1 ;i <= head ;i ++)
{
if (u[i].name == k)
{
return i ;
}
}
return -1 ;
}
bool cmp(User a, User b)
{
return a.score > b.score ;
}
int s ;
int main()
{
cin >> n ;
for (int i = 1 ;i <= n ;i ++)
{
cin >> a[i] ;
cin >> b[i] ;
int res = find(a[i]) ;
if (res == -1)
{
head ++ ;
u[head].name = a[i] ;
u[head].score += b[i] ;
}
else
{
u[res].score += b[i] ;
}
}
sort(u + 1, u + n + 1, cmp) ;
s = u[1].score ;
for (int i = 1 ;i <= head ;i ++)
{
u[i].score = 0 ;
}
for (int i = 1 ;i <= n; i ++)
{
u[find(a[i])].score += b[i] ;
for (int j = 1 ;j <= head ;j ++)
{
if (u[j].score >= s)
{
cout << u[j].name << endl ;
return 0 ;
}
}
}
return 0 ;
}