当前位置:网站首页>ZZULIOJ 1116 删除元素【删】
ZZULIOJ 1116 删除元素【删】
2022-08-10 10:01:00 【安苒_】
1116: 删除元素
时间限制: 1 Sec 内存限制: 128 MB
提交: 20393 解决: 12008
[状态] [讨论版] [提交] [命题人:admin]题目描述
输入一个递增有序的整型数组A有n个元素,删除下标为i的元素,使其仍保持连续有序。注意,有效下标从0开始。 定义如下两个函数分别实现删除元素操作和数组输出操作。
void del(int a[], int n, int i); /*删除数组a中下标为i的元素*/
void PrintArr(int a[], int n); /*输出数组a的前n个元素*/输入
输入分三行,第一行是一个整数n(n<10),第二行是n个整数,第三行是要删除元素的下标i,i>=0。
输出
输出删除下标为i的元素后的数组元素,数据之间用空格隔开。
样例输入 Copy
6 1 3 5 7 9 11 3样例输出 Copy
1 3 5 9 11提示
请注意当数组只有一个元素,并且被删除之后,则数组为空,此时应该是没有任何输出。常见错误是在此种情况下输出了第一个元素或输出了回车。
#include <stdio.h>
void del(int a[], int n, int i);
void PrintArr(int a[], int n);
int main()
{
int n;
int i,j;
int a[20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int idex;
scanf("%d",&idex);
//因为题目有要求,如果最后没有元素,不输出,所以这里加一个判断
if(n==1)
return 0;
del(a,n,idex);
PrintArr(a,n);
return 0;
}
//删除方法:定位到要删除的位置,依次往前移动(for循环实现)
//插入方法:定位到要插入的位置,从最后一个开始往后移动
void del(int a[], int n, int i)
{
int j;
for(j=i;j<n;j++)
{
a[j]=a[j+1];
}
}
void PrintArr(int a[], int n)
{
int i;
for(i=0;i<n-1;i++)
printf("%d ",a[i]);
}
解题关键分析:
(1)删除操作
定位到要删除的位置,依次往前移动(for循环实现)
补充:插入操作:定位到要插入的位置,往后移动
边栏推荐
猜你喜欢
The Generation of Matlab Symbolic Functions and the Calculation of Its Function Values
第三章 搜索与图论(三)
Chapter 3 Search and Graph Theory (3)
Plot temperature curves; QChart,
91.(cesium之家)cesium火箭发射模拟
Message Queuing Overview
The first offline workshop in 2022!Data application experience day for application developers is coming | TiDB Workshop Day
Excel draws statistical graphs
CentOS和Ubantu的Mysql主从配置
90.(cesium之家)cesium高度监听事件
随机推荐
LeetCode Algorithm 914. 卡牌分组
keepalived:常见问题
解决ASP.NET Core在Task中使用IServiceProvider的问题
《广州市公路工程安全生产监督管理办法》印发,从六大方面进行修订
多元线性回归分析(Stata)
90.(cesium之家)cesium高度监听事件
2022年固定资产管理系统的概况
Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
07 【动态组件 组件注册】
讯飞翻译机抢镜背后,跨语种沟通迈入全新时代
【数据架构】概念数据模型和逻辑数据模型有什么区别
keepalived:主备配置
「敏捷建模」纪律:敏捷设计理念
LeetCode Algorithm 1472. 设计浏览器历史记录
Static关键字及应用,继承的概念
Payment x Aggregation x Ledger Separation - The Way to Avoid Risk of "Erqing" on the Return Platform
武功修炼:招式
How to understand the difference between BIO, NIO, and AIO
2022.8.9-----leetcode.1413
VBA: Inputbox Function and Inputbox Method