题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201?tpId=37&tqId=21231&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
#include <stdio.h>
int main() {
unsigned int n;
scanf("%d",&n);
typedef struct{
int ind;
int val;
int tap;
}x;
x a[n],temp;
int i,j;
for(i=0;i<n;i++){
scanf("%d %d",&a[i].ind,&a[i].val);
a[i].tap=0;
}
for(i=n-1;i>0;i--){
for(j=0;j<i;j++){
if(a[j].ind>a[j+1].ind){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n-1;i++){
if(a[i].tap!=1){
for(j=i+1;j<n;j++){
if(a[j].tap!=1&&a[j].ind==a[i].ind){
a[i].val+=a[j].val;
a[j].tap=1;
}
}
}
}
for(i=0;i<n;i++){
if(a[i].tap!=1){
printf("%d %d\n",a[i].ind,a[i].val);
}
}
return 0;
}