描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据完成图书信息表的创建,然后将图书按照价格降序排序,逐行输出排序后每本图书的信息。
输入
输入n+1行,前n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。最后第n+1行是输入结束标志:0 0 0(空格分隔的三个0)。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计n行,每行是一本图书的信息(书号、书名、价格),书号、书名、价格用空格分隔。其中价格输出保留两位小数。
样例输入1 复制
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
样例输出1
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787302164340 Operating-System 50.00
9787822234110 The-C-Programming-Language 38.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257646 Data-Structure 35.00
9787302219972 Software-Engineer 32.00
这题我在dev和vs2010上交没问题,但是oj过不了,
发现我校是G++4.3版本,于是我在sort的插件cmp
里面加入const,于是代码就过了,
还有,就是scanf可以直接换成cin输入,测试没问题。
过的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#define MAXSIZE 10000
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct
{
Book *elem;
int length;
}SqList;
SqList L;
bool IninList(SqList &L)
{
L.elem=new Book[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
int cmp(const Book &a,const Book &b)
{
if(a.price>b.price)
return 1;
else
return 0;
}
int main()
{
IninList(L);
int start=0;
while(scanf("%s%s%f",L.elem[start].no,L.elem[start].name,&L.elem[start].price))
{
if(L.elem[start].no[0]=='0'&&L.elem[start].name[0]=='0'&&(L.elem[start].price-0.0<1e-6))
break;
else
{
start++;
L.length++;
}
}
sort(L.elem,L.elem+L.length,cmp);
for(int i=0;i<=L.length-1;i++)
{
printf("%s %s %.2f\n",L.elem[i].no,L.elem[i].name,L.elem[i].price);
}
delete [] L.elem;
return 0;
}