Posted on 2009-03-23 19:46
Hero 阅读(107)
评论(0) 编辑 收藏 引用 所属分类:
代码如诗--ACM
1 //H Accepted 31 10748 1833 C++
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include <iostream>
7 using namespace std ;
8
9 struct NODE
10 {
11 char name[100] ;
12 int year ;
13 int price ;
14 };
15 struct NODE book[100000] ;
16
17 int inn ;
18
19 int cmp1( const void *a, const void *b )
20 {
21 struct NODE *c = (struct NODE *)a ;
22 struct NODE *d = (struct NODE *)b ;
23
24 if( c->year != d->year )
25 return c->year - d->year ;
26 else if( strcmp(c->name, d->name) != 0 )
27 return strcmp( c->name, d->name ) ;
28 else
29 return c->price - d->price ;
30 }
31
32 int cmp2( const void *a, const void *b )
33 {
34 struct NODE *c = (struct NODE *)a ;
35 struct NODE *d = (struct NODE *)b ;
36
37 if( c->price != d->price )
38 return c->price - d->price ;
39 else if( strcmp(c->name, d->name) != 0 )
40 return strcmp( c->name, d->name ) ;
41 else
42 return c->year - d->year ;
43 }
44 int cmp3( const void *a, const void *b )
45 {
46 struct NODE *c = (struct NODE *)a ;
47 struct NODE *d = (struct NODE *)b ;
48
49 if( strcmp(c->name, d->name) != 0 )
50 return strcmp( c->name, d->name ) ;
51 else if( c->year != d->year )
52 return c->year - d->year ;
53 else
54 return c->price - d->price ;
55 }
56
57 int main()
58 {
59 int year, price ; char name[100] ;
60
61 while( cin >> inn && inn )
62 {
63 for( int i=1; i<=inn; i++ )
64 {
65 cin >> book[i].name ;
66 cin >> book[i].year ;
67 cin >> book[i].price ;
68 }
69
70 char str[100] ; cin >> str ;
71 if( strcmp( str, "Year" ) == 0 )
72 {
73 qsort( book+1, inn, sizeof(book[1]), cmp1 ) ;
74 }
75 else if( strcmp( str, "Price" ) == 0 )
76 {
77 qsort( book+1, inn, sizeof(book[1]), cmp2 ) ;
78 }
79 else
80 {
81 qsort( book+1, inn, sizeof(book[1]), cmp3 ) ;
82 }
83
84 for( int i=1; i<=inn; i++ )
85 {
86 cout << book[i].name << " " ;
87 cout << book[i].year << " " ;
88 cout << book[i].price << endl ;
89 }
90 cout << endl ;
91 }
92
93 return 0 ;
94 }