zoj 2727 List the Books(三级排序 = =水题)时间:2021-08-04 21:15:08给书的名字,年份,价格排序。 名字优先级大于年份大于价格。 无语死了都 = =。用sort排序,返回值是0或者1,返回2都不行 = =。这个记住了。 #include <stdio.h>#include <stdlib.h>#include <iostream>#include <string.h>#include <algorithm>using namespace std;typedef struct{char name[85];int year,price;}book;book b[105];int cmpname(book a,book b){if( strcmp(a.name,b.name) == 0 )if( a.year == b.year )return a.price < b.price;elsereturn a.year < b.year;if( strcmp(a.name,b.name) == -1 )return 1;elsereturn 0;}int cmpyear(book a,book b){if( a.year == b.year )if( strcmp(a.name,b.name) == 0 )return a.price < b.price;elsereturn cmpname(a,b);return a.year < b.year;}int cmpprice(book a,book b){if( a.price == b.price )if( strcmp(a.name,b.name) == 0 )return a.year < b.year;elsereturn cmpname(a,b);return a.price < b.price;}int main(){int n,i;char cmp[10];int flag = 0;while( scanf("%d",&n)!=EOF && n ){if( flag ) printf("/n");flag = 1;for(i=0; i<n; i++)scanf("%s %d %d",b[i].name, &b[i].year,&b[i].price);scanf("%s",cmp);if( strcmp(cmp,"Price") == 0 )sort(b,b+n,cmpprice);elseif( strcmp(cmp,"Name") == 0 )sort(b,b+n,cmpname);elsesort(b,b+n,cmpyear);for(i=0; i<n; i++)printf("%s %d %d/n",b[i].name,b[i].year,b[i].price);}return 0;}