题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include "stdio.h"
int main()
{
int number=0,count = 0;
scanf("%d",&number);
int buf[number][2];
char flag=0;
for(int i=0;i<number;i++)
{
int temp1,temp2;
flag = 0;
scanf("%d %d",&temp1,&temp2);
for(int j=0;j<count;j++)
{
if(temp1 == buf[j][0])
{
buf[j][1]+=temp2;
flag =1;
}
}
if(flag == 0)
{
buf[count][0] = temp1;
buf[count][1] = temp2;
count ++;
}
}
int i=0;
int j=0;
for(i=0;i<count-1;i++)
{
for(j=0;j<count-1-i;j++)
if(buf[j][0]>buf[j+1][0])
{
int temp1,temp2;
temp1 = buf[j][0];
temp2 = buf[j][1];
buf[j][0] = buf[j+1][0];
buf[j][1] = buf[j+1][1];
buf[j+1][0] = temp1;
buf[j+1][1] = temp2;
}
}
for(j=0;j<count;j++)
{
printf("%d %d\n",buf[j][0],buf[j][1]);
}
return 0;
}
保存+冒泡 应该能搞定吧
