当前位置:网站首页>《程序设计基础》 第十章 函数与程序结构 6-12 有序表的简单增删查操作 (20 分)
《程序设计基础》 第十章 函数与程序结构 6-12 有序表的简单增删查操作 (20 分)
2022-07-20 16:33:00 【茶然o】
首先输入一个正整数N(1≤N≤1000)和一个无重复元素的、从小到大排列的、N个元素的有序表,然后在屏幕上显示以下菜单(编号和选项):
[1] Insert
[2] Delete
[3] Query
[Other option] End
用户可以反复对该有序表进行插入、删除和查找操作,也可以选择结束。当用户输入编号1~3和相关参数时,将分别对该有序表进行插入、删除和查找操作,输入其他编号,则结束操作。
本题要求实现3个函数,分别在有序表(数组)中插入、删除、查找一个值。
函数接口定义:
void insert(int a[ ], int value);
void delete(int a[ ], int value);
void query(int a[ ], int value);
函数insert
在有序数组a
中插入一个值为value
的元素,并调用函数print_array(a)
输出插入后的有序数组a
。
函数delete
删除有序数组a
中等于value
的元素,并调用函数print_array(a)
输出删除后的有序数组a
;如果在数组a
中没有找到值为value
的元素,则输出Deletion failed.
。
函数query
用二分法在有序数组a
中查找元素value
,如果找到,则输出相应的下标;如果没有找到,则输出Not found.
裁判测试程序样例:
#include<stdio.h>
#define MAXN 1000
int Count = 0; /* 用全局变量Count表示数组a中待处理的元素个数 */
void select(int a[], int option, int value); /* 决定对有序数组a进行何种操作的控制函数 */
int input_array(int a[ ]); /* 输入有序数组a的函数 */
void print_array(int a[ ]); /* 输出有序数组a的函数 */
void insert(int a[ ], int value);
void delete(int a[ ], int value);
void query(int a[ ], int value);
int main(void)
{
int option, value, a[MAXN];
if(input_array(a) == -1){
return 0;
}
printf("[1] Insert\n");
printf("[2] Delete\n");
printf("[3] Query\n");
printf("[Other option] End\n");
while (1) {
scanf("%d", &option);
if (option < 1 || option > 3) {
break;
}
scanf("%d", &value);
select(a, option, value);
printf("\n");
}
printf("Thanks.");
return 0;
}
/* 控制函数 */
void select(int a[ ], int option, int value)
{
switch (option) {
case 1:
insert(a, value);
break;
case 2:
delete(a, value);
break;
case 3:
query(a, value);
break;
}
}
/* 有序表输入函数 */
int input_array(int a[ ])
{
scanf("%d", &Count);
for (int i = 0; i < Count; i ++) {
scanf("%d", &a[i]);
if(i > 0 && a[i] <= a[i-1]){
printf("Error");
return -1;
}
}
return 0;
}
/* 有序表输出函数 */
void print_array(int a[ ])
{
for (int i = 0; i < Count; i ++) {
if(i == 0){
printf("%d", a[i]);
}else{
printf(" %d", a[i]);
}
}
}
/* 请在这里填写答案 */
输入样例1:
插入一个值,输入数据如下:
6 -2 3 7 9 101 400
1
96
0
输出样例1:
[1] Insert
[2] Delete
[3] Query
[Other option] End
-2 3 7 9 96 101 400
Thanks.
输入样例2:
删除一个值,输入数据如下:
6 -2 3 7 9 101 400
2
400
0
输出样例2:
[1] Insert
[2] Delete
[3] Query
[Other option] End
-2 3 7 9 101
Thanks.
输入样例3:
查询一次,输入数据如下:
6 -2 3 7 9 101 400
3
-2
0
输出样例3:
[1] Insert
[2] Delete
[3] Query
[Other option] End
0
Thanks.
输入样例4:
连续增删查,输入数据如下:
6 -2 3 7 9 101 400
2
101
1
-10
3
101
0
输出样例4:
[1] Insert
[2] Delete
[3] Query
[Other option] End
-2 3 7 9 400
-10 -2 3 7 9 400
Not found.
Thanks.
void insert(int a[ ], int value){
int i,j;
for (i = 0;i < Count;i++){
if (value < a[i]){
break;
}
}
for(j = Count - 1; j >= i;j--){
a[j + 1] = a[j];
}
a[i] = value;
Count++;
print_array(a);
}
void delete(int a[ ], int value){
int i,index = -1;
for (i = 0;i < Count;i++){
if (value == a[i]){
index = i;
break;
}
}
if (index == -1){
printf ("Deletion failed.");
return;
}else{
for (i = index;i < Count - 1;i++){
a[i] = a[i + 1];
}
}
Count--;
print_array(a);
}
void query(int a[ ], int value){
int mid,left = 0,right = Count - 1;
while (left <= right){
mid = (left + right) / 2;
if (value == a[mid]){
printf ("%d",mid);
return;
}else if(value < a[mid]){
right = mid -1;
}else{
left = mid + 1;
}
}
printf ("Not found.");
}
边栏推荐
- 21_ life cycle
- Install MySQL through docker in centos7
- 微信小程序 02 Hello miniProgram
- Advantages of stable tripartite payment channels
- ODBC 执行存储过程获取返回值
- 【无标题】
- Kubevirt manages virtual machines
- Video object segmentation
- Pycharm configuration
- JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
猜你喜欢
"Hisense's B-side" technology exhibition opens! Hisense B2B represents the first collective appearance of products!
TNN notes
WPF 使用PathGeometry画时针和分针
Error when wmware enables virtualization function
使用Kettle连接MySQL数据库错误
李宏毅老师2020年深度学习系列讲座笔记2
Self study notes on Bayesian probability and Bayesian networks and Bayesian causal networks
Programmers are new to the workplace, how to plan their career?
Separable Convolution可分离卷积
正负折线图凸显数据趋势变化
随机推荐
网络安全学习(九)综合实验&PKI
Squeeze-and-Excitation Networks
Zhang Xiaoquan, are you wronged?
Ncnn OP forward code learning
網絡安全學習(七)IIS
Self study notes on Bayesian probability and Bayesian networks and Bayesian causal networks
静态代码分析是如何工作的
Hydrogen future, China hydrogen energy alliance held the launching ceremony of 2022 hydrogen energy specialty and new entrepreneurship competition
"Hisense's B-side" technology exhibition opens! Hisense B2B represents the first collective appearance of products!
微信小程序 22 recommend和songDetail初步搭建
find_ var.sh
Video object segmentation
RichTextbox保存为图片
Musk: I uploaded my brain to the cloud. Sorry, 404
2021/7/24 SVM 2021/7/26 后门学习&对抗神经网络
What if there are more than 1 million messages in the message queue of highly paid programmers & interview questions series 126? How to ensure the consistency of messages?
MNN tutorials
wpf 打开外部程序并在需要时激活
网络安全学习(二)IP地址
Network Security Learning (x) simple test process of penetration