数据结构算法练习题

阅读: 评论:0

数据结构算法练习题

数据结构算法练习题

第一题

根据所给信息查找顺序表中的信息,并输出所有找到的数据及其个数 

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
2
Java-Programming-Language
Data-Structure

描述

定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的最爱图书的名字,查找最爱的图书,输出相应图书的信息。

输入

总计n+m+2行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。然后输入m+1行,其中,第一行是一个整数m,代表查找m次,后m行是每次待查找的最爱图书名字。

输出

若查找成功: 总计输出m*(k+1)行,对于每一次查找,第一行是最爱图书数目(同一书名的图书可能有多本),后k行是最爱图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,其中价格输出保留两位小数。 若查找失败: 只输出以下提示:抱歉,没有你的最爱!

输出:

Sorry,there is no your favourite!
2
9787302257646 Data-Structure 35.00
9787302257800 Data-Structure 62.00

代码:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define error 0
# define Maxsize 500typedef struct Book
{string IBNS;string NAME;float PRICE;
};
typedef struct List
{Book* elem;int length;
};int InitList_L(List& L);
int InsertList_L(List& L);
int FindList_L(List L);int main()
{List L;InitList_L(L);InsertList_L( L);FindList_L(L);
}int InitList_L(List& L)
{L.elem = new Book[Maxsize];if (!L.elem) exit(0);//分配空间失败,程序结束L.length = 0;//初始化表长为0return OK;
}int InsertList_L(List& L)
{int Num=0;cin >> Num;for (int i = 0; i < Num; i++){cin >> L.elem[i].IBNS >> L.elem[i].NAME >> L.elem[i].PRICE;L.length++;}return OK;
}int FindList_L(List L)
{int Num = 0;cin >> Num;int flag = 1;for (int i = 0; i < Num; i++){string Fbook;cin >> Fbook;int Num1 = 0;for (int j = 0; j < L.length - 1; j++)if (Fbook == L.elem[j].NAME){Num1++;}if (Num1)cout << Num1 << endl;for (int j = 0; j < L.length - 1; j++){if (Fbook == L.elem[j].NAME){cout << L.elem[j].IBNS << " " << L.elem[j].NAME << " " << fixed << setprecision(2) << L.elem[j].PRICE << endl;flag = 0;}}if (flag)cout << "Sorry,there is no your favourite!" << endl;}return OK;
}

第二题

定义顺序表,完成信息创建之后再插入新元素

输入

7
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
2
9787822234110 The-C-Programming-Language 38.00

输出:代码

9787302257646 Data-Structure 35.00
9787822234110 The-C-Programming-Language 38.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

代码:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define error 0
#define Maxsize 500typedef struct
{string IBNS;string NAME;float PRICE;
}Book;typedef struct
{Book* elem;int length;
}List;int InitList_L(List& L);
int InsertList_L(List& L);
int Add_ByLocate(List& L);int main()
{List L;InitList_L(L);InsertList_L(L);Add_ByLocate(L);return 0;
}int Add_ByLocate(List& L)
{int num;cin >> num;if (num<1 || num>L.length + 1)cout << "Sorry,the position to be inserted is invalid!" << endl;else{string IBNS, NAME;float  PRICE;cin >> IBNS >> NAME >> PRICE;for (int i = L.length - 1; i >= num - 1; i--){L.elem[i + 1] = L.elem[i];}L.elem[num - 1].IBNS = IBNS;L.elem[num - 1].NAME = NAME;L.elem[num - 1].PRICE = PRICE;L.length++;for(int j=0;j<L.length;j++)cout << L.elem[j].IBNS << " " << L.elem[j].NAME << " " << fixed << setprecision(2) << L.elem[j].PRICE << endl;return OK;}}
int InitList_L(List& L)
{L.elem = new Book[Maxsize];if (!L.elem) exit(0);L.length = 0;;return OK;
}
int InsertList_L(List& L)
{int num;cin >> num;for (int i = 0; i < num; i++){cin >> L.elem[i].IBNS >> L.elem[i].NAME >> L.elem[i].PRICE;L.length++;}return OK;
}

第三题

插入之后又进行删除

输入:输入元素的位置进行删除

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
2

输出:

9787302257646 Data-Structure 35.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

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define error 0
#define Maxsize 500typedef struct
{string IBNS;string NAME;float PRICE;
}Book;typedef struct
{Book* elem;int length;
}List;int InitList_L(List& L);
int InsertList_L(List& L);
int Add_ByLocate(List& L);
int DeleteList_L(List& L);int main()
{List L;InitList_L(L);InsertList_L(L);//Add_ByLocate(L);DeleteList_L(L);return 0;
}int DeleteList_L(List& L)
{int num;cin >> num;if (num<1 || num>L.length)cout << "Sorry,the position to be deleted is invalid!" << endl;else {for (int i = num; i < L.length; i++)L.elem[i - 1] = L.elem[i];//删除以及以前的元素向前移动L.length--;for (int j = 0; j < L.length; j++)cout << L.elem[j].IBNS << " " << L.elem[j].NAME << " " << fixed << setprecision(2) << L.elem[j].PRICE << endl;}return OK;
}int Add_ByLocate(List& L)
{int num;cin >> num;if (num<1 || num>L.length + 1)cout << "Sorry,the position to be inserted is invalid!" << endl;else{string IBNS, NAME;float  PRICE;cin >> IBNS >> NAME >> PRICE;for (int i = L.length - 1; i >= num - 1; i--){L.elem[i + 1] = L.elem[i];//插入以及之后的位置后移}L.elem[num - 1].IBNS = IBNS;L.elem[num - 1].NAME = NAME;L.elem[num - 1].PRICE = PRICE;L.length++;for(int j=0;j<L.length;j++)cout << L.elem[j].IBNS << " " << L.elem[j].NAME << " " << fixed << setprecision(2) << L.elem[j].PRICE << endl;return OK;}}
int InitList_L(List& L)
{L.elem = new Book[Maxsize];if (!L.elem) exit(0);L.length = 0;;return OK;
}
int InsertList_L(List& L)
{int num;cin >> num;for (int i = 0; i < num; i++){cin >> L.elem[i].IBNS >> L.elem[i].NAME >> L.elem[i].PRICE;L.length++;}return OK;
}

本文发布于:2024-01-31 08:23:19,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170666062527112.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据结构   练习题   算法
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23