这道题有个continue会跳过for的第三段,是去重的关键
描述
出版社出版的任何一本图书的书号(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.001
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
61#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define OK 1
using namespace std;
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct LNode
{
Book data;
struct LNode *next;
}LNode,*LinkList;
LinkList L;
bool InintList(LinkList &L)
{
L=new LNode;
L->next=NULL;
return OK;
}
int main()
{
int num;
InintList(L);
cin>>num;
LNode *pre=L;//pre非常关键,这个是为了让链表最后一端为NULL
for(int i=0;i<num;i++)
{
LNode *p=new LNode;
scanf("%s%s%f",p->data.no,p->data.name,&p->data.price);
pre->next=p;
pre=p;
}
pre->next=NULL;//结尾加入NULL
//之前创建的链表没有NULL节点,不太好,这里会有问题,但是现在有NULL了
LNode *it=L->next->next;
pre=L->next;
for(;it!=NULL;it=it->next,pre=pre->next)
{
if(strcmp(pre->data.no,it->data.no)==0)
{
pre->next=it->next;
it=it->next;
num--;
continue;//这里会跳过接下来的操作,也就是说for第三段的也不执行
}
}
it=L->next;
cout<<num<<endl;
while(it)
{
printf("%s %s %.2f\n",it->data.no,it->data.name,it->data.price);
it=it->next;
}
return 0;
}