当前位置:网站首页>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循环实现)
补充:插入操作:定位到要插入的位置,往后移动
边栏推荐
- LeetCode Algorithm 1403. 非递增顺序的最小子序列
- Basic concepts, structures, and classes of thread pools
- 【物联网架构】最适合物联网的开源数据库
- IDEA中xml文件头报错:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
- 重学冒泡排序
- VBA: 采用Combox控件实现二级下拉菜单功能
- 06 【生命周期 模板引用】
- Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
- 多租户技术
- 俄罗斯宣布临时禁止进口摩尔多瓦植物产品
猜你喜欢
Payment x Aggregation x Ledger Separation - The Way to Avoid Risk of "Erqing" on the Return Platform
Message Queuing Overview
10 【异步组件 组合式函数(hooks)】
Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
[Internet of Things Architecture] The most suitable open source database for the Internet of Things
[Data Architecture] Distributed Data Grid as a Solution for Centralized Data Monolith
腾讯发布四足机器人 Max 二代版本,梅花桩上完成跳跃、空翻
Shell functions and arrays
LCD DRM驱动框架分析一
2022-08-09 第六小组 瞒春 学习笔记
随机推荐
在兄弟连战狼班参加PHP培训做行业领先人才
数据库中的schema
Payment x Aggregation x Ledger Separation - The Way to Avoid Risk of "Erqing" on the Return Platform
CentOS和Ubantu的Mysql主从配置
ESP8266-Arduino编程实例-MQ-9 一氧化碳可燃气体传感器驱动
JS高级 之 使用 Iterator - Generator
GO文件相关操作使用
VBA:获取指定数值在指定一维数组中的位置
90.(cesium之家)cesium高度监听事件
lua初学
多租户技术
属性动画QPropertyAnimation
反射效率为什么低?
腾讯云校园大使开始招募啦,内推名额和奖金等你来拿
「业务架构」TAGAF建模:业务服务/信息图
「第二部:容器和微服务架构」(1) 基于容器应用架构设计原则
效率开发目录
VBA: 采用Combox控件实现二级下拉菜单功能
Guo Jingjing's personal chess teaching, the good guy is a robot
Chapter 3 Search and Graph Theory (3)