当前位置:网站首页>C语言一维数组练习——将m个元素移动到数组尾部
C语言一维数组练习——将m个元素移动到数组尾部
2022-08-02 14:02:00 【iccoke】
将m个元素放到数组尾部
这里我们的思路是将数组的前m个元素存储到另一个数组后然后再将剩余元素往前移动m位,然后再将存储的元素加到数组的尾部即可
具体代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<assert.h>
void moveelement(int *arr,int *brr,int m,int len) {
int i = 0; int j = 0;
int k = 0;
assert(m < len);
for ( i = 0; i < m; i++) {
brr[i] = arr[i];
}
for ( j = m; j < len; j++) {
arr[j - m] = arr[j];
}
for (int n = len - m; n < len; n++) {
arr[n] = brr[k];
k++;
}
return;
}
int main() {
int arr[] = { 1,2,3,4,5,6 };
int brr[10];
int m = 2;
int len = sizeof(arr) / sizeof(arr[0]);
moveelement(arr, brr, m, len);
for (int w = 0; w < len; w++) {
printf("% d", arr[w]);
}
}将前两个元素移动到尾部

但是这个代码开辟了新的内存空间,所以这个代码的空间复杂度又比较大,因此我们需要进行优化
我们可以采取另外一种思路
关于时间复杂度和空间复杂度
算法的时间复杂度与空间复杂度_iccoke的博客-CSDN博客
将整个数组扭转,再根据要求再扭转
例如
将 1,2,3,4,5,6的前2位移动到尾部
先全部扭转,结果为
6,5,4,3,2,1
再分别扭转前四个和后两个即可
扭转前四个结果为
3,4,5,6,2,1
最后扭转后两位结果为
3,4,5,6,1,2
这样就以一种比较低的空间复杂度完成了我们的目的
具体代码如下
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<assert.h>
void Reverse(int* arr, int begin, int end) {
assert(arr != NULL && end >= begin);
int temp;
int n = (end - begin + 1) / 2;
for (int i = 0; i < n; i++,begin++,end--) {
temp=arr[end];
arr[end] = arr[begin];
arr[begin] = temp;
}
}
void moveelement(int* arr, int len, int m) {
assert(arr != NULL);
Reverse(arr, 0, len - 1);
Reverse(arr, 0, len - m - 1);
Reverse(arr, len - m, len - 1);
}
int main() {
int arr[] = { 1,2,3,4,5,6 };
int len = sizeof(arr) / sizeof(arr[0]);
moveelement(arr, len, 2);
for (int i = 0; i < len ; i++) {
printf("%5d", arr[i]);
}
return 0;
}结果如下

边栏推荐
- [ROS] (06) ROS Communication - Topic Communication
- chapter6可视化(不想看版)
- Paddle window10 environment using conda installation
- 第三单元 视图层
- [ROS] (05) ROS Communication - Node, Nodes & Master
- Chapter6 visualization (don't want to see the version)
- Flask请求应用上下文源码分析
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
- chapter7
- Implementation of redis distributed lock and watchdog
猜你喜欢
随机推荐
Paddle window10 environment using conda installation
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id ‘c
Haystack的介绍和使用
Network pruning (1)
STM32(F407)—— 堆栈
drf source code analysis and global catch exception
[ROS] (01) Create ROS workspace
Unit 11 Serializers
Raj delivery notes - separation 第08 speak, speaking, reading and writing
Unit 12 associated serialization
Unit 7 ORM table relationships and operations
瑞吉外卖笔记——第10讲Swagger
Unit 3 view layer
Camera Hal(Hal3)层修改Preview流
[ROS] (05) ROS Communication - Node, Nodes & Master
xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
8576 顺序线性表的基本操作
static关键字3种作用,简单粗暴对比,好理解
PHP open source customer service system _ online customer service source code PHP
Verilog Learning Series




创建ROS工作空间](/img/2a/11e5023ef6d052d98b4090d2eea017.png)
ROS通信 —— 话题(Topic)通信](/img/21/d79f2c4e246eb9ea39df9c7435bb36.png)

CMakeLists.txt详解](/img/34/577c729f06748f625ca0c223496d48.png)

ROS通信 —— 节点,Nodes & Master](/img/f5/c541259b69a0db3dc15a61e87f0415.png)