题解 | #牛牛的书#
牛牛的书
https://www.nowcoder.com/practice/30bb969e117b4f6d934d4b60a2af7489
#include<malloc.h>
#include <stdio.h>
#include<string.h>
struct Book //构造书本结构体
{
char name[114];
int price;
};
void sort_price(struct Book*p,int len) //升序冒泡排序
{
struct Book t;
int i,j;
for(i=0;i<len;i++)
{
for(j=0;j<len-1-i;j++)
{
if(p[j].price>p[j+1].price)
{
t = p[j];
p[j] = p[j+1];
p[j+1] = t;
}
}
}
}
void OutputName(struct Book* p,int len) //升序输出书本名
{
int i;
for(i=0;i<len-1;i++)
printf("%s\n",p[i].name);
printf("%s",p[i].name);
}
int main()
{
int len;
scanf("%d",&len);
struct Book* pArr = (struct Book*)malloc(len*sizeof(struct Book));
for(int i=0;i<len;i++)
{
scanf("%s",pArr[i].name);
scanf("%d",&pArr[i].price);
}
sort_price(pArr, len);
OutputName(pArr, len);
}