自己写的。C语言。题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include<stdio.h> int main() { int index[500], value[500]; int n, i, IndexMin = 11111112, j, index_temp, value_temp, insert = 0; for (i = 0; i < 500; i++) { index[i] = 11111112; } scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &index_temp, &value_temp); j = 0; while (j < insert) { if (index[j] == index_temp) { value[j] += value_temp; break; } j++; } if (j == insert) { index[insert] = index_temp; value[insert++] = value_temp; } } while (1) { IndexMin = 11111112; for (i = 0; i < 500; i++) { if (index[i] < IndexMin) { IndexMin = index[i]; } } if (IndexMin == 11111112) { break; } for (i = 0; i < 500; i++) { if (index[i] == IndexMin) { printf("%d %d\n", index[i], value[i]); index[i] = 11111112; } } } return 0; }