访问次数:

基于顺序存储结构的图书信息表的图书去重 | Wenji's blog
头部背景图片
WenJi's blog |
WenJi's blog |

基于顺序存储结构的图书信息表的图书去重

描述
出版社出版的任何一本图书的书号(ISBN)都是唯一的,即图书表中不允许包含书号重复的图书。定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建(书号可能重复),然后进行图书的去重,即删除书号重复的图书(只保留第一本),最后输出去重后所有图书的信息。

输入
总计输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格(书号可能重复)。其中书号和书名为字符串类型,价格为浮点数类型。

输出
总计输出m+1行(m≤n),其中,第一行是去重后的图书数目,后m行是去重后图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,其中价格输出保留两位小数。

样例输入1 复制
9
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
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
样例输出1
8
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

##这道题我写了一会,发现这道题有一个点易错。
就是去重的条件的限制,L.length到底在循环中
到底是-1还是不减,还有到底是<还是<=这个在条件
中是很重要的。还有由于L.length要不断更新,因为
这是很重要的一件事,不然没法做到所有的去重,不过
我仍然认为我这是一个最暴力的解法,并不聪明。
我是查到重之后,就进行一次前移。
以后有更好的方法我会更新。

正确的代码

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
59
60
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#define MAXSIZE 1000
#define OK 1
#define ERROR 0
using namespace std;
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct
{
Book *elem;
int length;
}SqList;
SqList L;
int N;
bool IninList(SqList &L)
{
L.elem = new Book[MAXSIZE];
if (!L.elem) exit(OVERFLOW);
L.length = N;
return OK;
}
int main()
{
cin>>N;
IninList(L);
for(int i=0;i<N;i++)
{
scanf("%s%s%f", L.elem[i].no, L.elem[i].name, &L.elem[i].price);
}
for(int i=0;i<L.length-1;i++)
{
Book book;
book=L.elem[i];
for(int j=i+1;j<L.length;j++)
{
if(strcmp(book.no,L.elem[j].no)==0)
{
for(int u=j;u<L.length-1;u++)
{
L.elem[u]=L.elem[u+1];
}
L.length--;
}
}
}
cout<<L.length<<endl;
for(int j=0;j<=L.length-1;j++)
printf("%s %s %.2f\n",L.elem[j].no,L.elem[j].name,L.elem[j].price);
delete[] L.elem;
return 0;
}