题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
int main(void) {
int num;
int array[500] = {0};
int hash[500] = {0};
scanf("%d", &num);
int a, b;
for (int i = 0; i < num; ++i) {
scanf("%d %d", &a, &b);
hash[i] = a;
array[i] = b;
//array[a] += b;
}
for (int j = 0; j < num; ++j) {
int tmp1 = 0, tmp2 = 0;
for (int k = j + 1; k < num; ++k) {
if (hash[j] > hash[k]) {
tmp1 = hash[k];
hash[k] = hash[j];
hash[j] = tmp1;
tmp2 = array[k];
array[k] = array[j];
array[j] = tmp2;
} else if (hash[j] == hash[k]) {
hash[k] = 0;
array[j] += array[k];
}
}
}
printf("%d %d\n", hash[0], array[0]);
for (int j = 1 ; j < num; ++j) {
if (hash[j] != 0)
printf("%d %d\n", hash[j], array[j]);
}
return 0;
}