# List the Books–解题报告

By | 2012/08/02

#include <stdio.h>
#include <string.h>
struct st //定义存书的信息的结构体
{
char name[100];
int year;
int price;
}book[200],sign;
int N;
void sort1()//name优先
{
int i,j,k;
for(i=0;i<N;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(strcmp(book[j].name,book[k].name)<0)
k=j;
if(strcmp(book[j].name,book[k].name)==0&&book[j].year<book[k].year)
k=j;
if(strcmp(book[j].name,book[k].name)==0&&book[j].year==book[k].year&&book[j].price<book[k].price)
k=j;
}
sign=book[k];
book[k]=book[i];
book[i]=sign;
}
}
void sort2()//year优先
{
int i,j,k;
for(i=0;i<N;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(book[j].year<book[k].year)
k=j;
if(book[j].year==book[k].year&&strcmp(book[j].name,book[k].name)<0)
k=j;
if(book[j].year==book[k].year&&strcmp(book[j].name,book[k].name)==0&&book[j].price<book[k].price)
k=j;
}
sign=book[k];
book[k]=book[i];
book[i]=sign;
}
}
void sort3()//price优先
{
int i,j,k;
for(i=0;i<N;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(book[j].price<book[k].price)
k=j;
if(book[j].price==book[k].price&&strcmp(book[j].name,book[k].name)<0)
k=j;
if(book[j].price==book[k].price&&strcmp(book[j].name,book[k].name)==0&&book[j].year<book[k].year)
k=j;
}
sign=book[k];
book[k]=book[i];
book[i]=sign;
}
}
int main()
{
int i,k=0;
char word[50];
while(scanf(“%d”,&N),N)
{
getchar();
memset(book,0,sizeof(book));
for(i=0;i<N;i++)
{
scanf(“%s %d%d”,book[i].name,&book[i].year,&book[i].price);
getchar();
}
memset(word,0,sizeof(word));
scanf(“%s”,word);
if(strcmp(word,”Name”)==0)
sort1();
if(strcmp(word,”Year”)==0)
sort2();
if(strcmp(word,”Price”)==0)
sort3();
if(k!=0)//处理空行
printf(“\n”);
for(i=0;i<N;i++)
printf(“%s %d %d\n”,book[i].name,book[i].year,book[i].price);
k++;
}
return 0;
}