题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <math.h> #include <stdio.h> int main() { int i=0,j=0,v=0,in=0; int n=0,m=0; struct data{ int index; int value; }temp; struct data D[501];//create 501 structs for data scanf("%d",&n);//get the number for datas for(i=0;i<n;i++){ int occupy = 0;//if occpied occupy = 1; scanf("%d %d",&in,&v); for (int k=0;k<j;k++){//遍历直到现有的所有room if(D[k].index==in){//遍历检查否在此之前存过该index的东西,如果有则不加1 occupy = 1;//has been occupied, skip next 'if' D[k].value+=v;//accumulate new value } } if (occupy==0)//havent found the same { D[j].index = in; D[j].value = v; j++;//when a new ROOM is occupied by a index and number, j+1 } }//get value for //delete function, set value to -1 for(i=0;i<j-1;i++){ for(m=i+1;m<j;m++){ if (D[i].index>D[m].index){ temp = D[i]; D[i] = D[m]; D[m] = temp;//swap the value between. } } } for(i=0;i<j;i++) { printf("%d %d\n",D[i].index,D[i].value); } return 0; }#打卡编程#