0105-Book Index

http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=0105

入力データーを構造体に入れてソート

typedef struct a {
	char b[50];
	int p;
} a;
a s[255];
int n, i, k;
int cmp(const a *s, const a *t) {
	k = strcmp(s->b, t->b);
	if (k)
		return k;
	return s->p - t->p;
}
int main() {
	while (scanf("%s %d", s[n].b, &s[n].p) + 1)
		n++;
	qsort(s, n, 56, (int(*)(const void*, const void*)) cmp);
	printf("%s\n%d", s[0].b, s[0].p);
	for (i = 1; i < n; i++)
		if (strcmp(s[i].b, s[i - 1].b))
			printf("\n%s\n%d", s[i].b, s[i].p);
		else
			printf(" %d", s[i].p);
	puts("");
	exit(0);
}