http://acm.hdu.edu.cn/showproblem.php?pid=2093
sscanf的使用,字符串对齐,快排;
# include <stdio.h> # include <stdlib.h> # define N 1005 typedef struct {char name[15]; int n; int s;} Player; Player p[N]; int n, m; int cmp(const void *xx, const void *yy) { Player *x, *y; x = (Player*)xx; y = (Player*)yy; if ((x->n) == (y->n)) return (x->s) < (y->s) ? -1:1; else return (x->n) > (y->n) ? -1:1; } int main() { char buf[20]; int i, c, x, y, ac; c = 0; scanf("%d%d", &n, &m); while (~scanf("%s", p[c].name)) { p[c].n = p[c].s = 0; for (i = 1; i <= n; ++i) { scanf("%s", buf); ac = sscanf(buf, "%d(%d)", &x, &y); if (ac == 2) { ++p[c].n; p[c].s += x+y*m; } else if (ac == 1 && x > 0) { ++p[c].n; p[c].s += x; } } c++; } qsort(p, c, sizeof(p[0]), cmp); for (i = 0; i < c; ++i) printf("%-10s %2d %4d\n", p[i].name, p[i].n, p[i].s); return 0; }
//