题目:HDU2093
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2093
结果:6次 (┬_┬);
要点:
- 复杂条件的sort函数的使用,即cmp函数的书写;
- sscanf的实际应用;
- 各种不熟练啊!多练多写;
- o( ̄皿 ̄///) !教练,我要学C++!
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; struct student { char name[11]; int k, s; }stu[1000]={0}; int cmp(student a, student b) { if(a.k!=b.k) return a.k>b.k; else if(a.s!=b.s) return a.s<b.s; else return strcmp(a.name, b.name)<0; } int main() { int m, n, i = 0, rs, a, b, j; char all[10]; scanf("%d%d", &m, &n); while (scanf("%s",stu[i].name)!=EOF) { for (j=0;j<m;j++) { scanf("%s",all); rs = sscanf(all,"%d(%d)", &a, &b); if(rs==1&&a>0) { stu[i].s += a; stu[i].k++; } else if(rs==2) { stu[i].k++; stu[i].s += a + b * n; } } i++; } sort(stu,stu+i,cmp); for(j=0;j<i;j++) printf("%-10s %2d %4d\n", stu[j].name, stu[j].k, stu[j].s); system("pause"); }