sort()cmp函数容易出错;
int cmp(Node a,Node b)
{
if (a.f!=b.f)return a.f>b.f;
else
{
int f=strcmp(a.name,b.name);//直接写成 return a.name-b.name就错
return f<0;
}
}
AC:
#include<stdio.h> #include<cstring> #include<algorithm> using namespace std; struct Node { char name[102]; int f; int need; }mo[10002]; int cmp(Node a,Node b) { if (a.f!=b.f)return a.f>b.f; else { int f=strcmp(a.name,b.name); return f<0; } } int main() { int n,m; while(scanf("%d %d",&n,&m)!=EOF) { int i; for(i=0;i<n;i++) scanf("%s %d %d",mo[i].name,&mo[i].f,&mo[i].need); sort(mo,mo+n,cmp); for(i=0;i<m;i++) { char str[102]; int count=1; scanf("%s",str); for(int j=0;j<n;j++) { if(strcmp(str,mo[j].name)!=0) count+=mo[j].need; else if(strcmp(str,mo[j].name)==0) { printf("%d ",count);break; } } } } return 0; }