题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> #include<ctype.h> void bubble_sort(int a1[], int b1[], int len) { int i, j, temp,temp1; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (a1[j] > a1[j + 1]) { temp = a1[j]; a1[j] = a1[j + 1]; a1[j + 1] = temp; temp1 = b1[j]; b1[j] = b1[j + 1]; b1[j + 1] = temp1; } } #include <stdio.h> int main() { int a[500], b[500], n, i, j, k = 0, a1[500], b1[500],len=1; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &a[i], &b[i]); } for (i = 1; i < n; i++) { for (j = 0; j < i; j++) { if (a[j] == a[i]) { b[j] = b[j] + b[i]; break; } } } a1[0] = a[0]; b1[0] = b[0]; for (i = 1; i < n; i++) { for (j = 0; j < i; j++) { if (a[j] == a[i]) { k++; } } if (k == 0) { a1[len] = a[i]; b1[len] = b[i]; len++; } k = 0; } bubble_sort(a1,b1,len); for (j = 0; j < len; j++) { printf("%d %d\n", a1[j], b1[j]); } return 0; }