当前位置:网站首页>将一串数字顺序后移
将一串数字顺序后移
2022-07-02 06:32:00 【程序员在旅途】
将一串数字顺序后移
一、题目描述
有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数(m<n)。(更多内容,可参阅程序员在旅途)
二、分析解答
这道题主要是编程逻辑的训练。涉及到数组、指针的知识点,本道题目也能很好的表达出选用不同的数据结构,对同一道题目会有完全不同的解题思路,编程复杂度也会有很大的差别。
方法一:采用数组数据结构存储数据。思路:①使用一个临时数组,把原数组中的后m个数字先复制到这个新建的数组中,再把原数组中的元素向后移动m个位置,最后把临时数组中的元素复制到原数组的开头位置。②使用一个和原数组长度一样的临时数组,将原数组的后m个数字复制到临时数组的前m位,然后再把原数组的剩余的数字复制到临时数组的后面,然后返回临时数组。
值得注意的是思路①②的临时数组空间大小是不一样的,编程上略有区别。还要注意数组下标的计算,这点很容易出错。
#include<stdio.h>
void shift(int *a,int n,int m)
{
int t[20];
int i;
for(i=0;i<n;i++)
{
t[i]=a[i];
}
for(i=0;i<m;i++)
{
a[i]=t[n-m+i];
}
for(i=m;i<n;i++)
{
a[i]=t[i-m];
}
}
int main()
{
int a[20];
int n,m;
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
shift(a,n,m);
for(i=0;i<n;i++)
{
printf("%d \t ",a[i]);
}
printf("\n");
return 0;
}
方法二:采用单链表数据结构存储数据。思路:将第(N - M)位置的元素作为首节点(也就是最后m个元素的首个元素节点),原链表首节点元素链接到原链表尾结点即可。
#include<stdio.h>
#include<stdlib.h>
typedef struct data_node{
int data;
struct data_node *next;
}Node;
int main(){
Node *head = NULL , *tail =NULL, *p = NULL;
int n,m,i=0;
//输入N个整数
scanf("%d",&n);
while(i++<n){
//申请节点所占用的内存空间
if( (p = (Node *)malloc(sizeof(Node))) == NULL){
printf("memery is not available");
exit(1);
}
scanf("%d", &(p->data));
p->next = NULL;
//当前申请的是第一个节点
if(head == NULL){
head = tail = p;
}else{
//链尾插值
tail->next = p;
tail = p;
}
}
//找到第(N-M)位置的前驱节点。
scanf("%d",&m);
i=0;
p = head;
while( i++<n -m -1){
p = p->next;
}
//注意这几个元素的交换。是实现这个题目的核心所在
//最后m个元素的首元素作为链头,原链表首节点元素链接到原链表尾结点。这样就实现了后移
tail->next = head;
head = p->next;
tail = p;
tail->next = NULL;
//输出处理后的数
p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return 0;
}
程序运行结果示意图:
三、小结
从上面的两种不同的方法解决同一问题我们可以看到,不同数据结构的选择,对编程思想的影响很大,数据结构的巧妙选择有时会大大的简化编程逻辑上的理解。同时,不同的数据结构还会对程序算法的时间复杂度和空间复杂度形成很大的差异。
边栏推荐
- gocv opencv exit status 3221225785
- Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
- Sqli labs level 1
- idea中注释代码取消代码的快捷键
- Rotating linked list (illustration)
- DWORD ptr[]
- Service de groupe minecraft
- 程序猿学英语-Learning C
- gocv图片裁剪并展示
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
猜你喜欢
Solid principle: explanation and examples
HCIA—数据链路层
Finishing the interview essentials of secsha system!!!
Analysis of the use of comparable, comparator and clonable interfaces
Linux安装Oracle Database 19c
Service de groupe minecraft
OpenFeign 簡單使用
Comparable,Comparator,Clonable 接口使用剖析
Minecraft空岛服开服
Web security -- Logical ultra vires
随机推荐
Shortcut key to comment code and cancel code in idea
sqli-labs第2关
Viewing JS array through V8
Linux binary installation Oracle database 19C
libusb的使用
ARP and ARP Spoofing
Tcp/ip - transport layer
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
旋转链表(图解说明)
Gateway is easy to use
gocv拆分颜色通道
Judge whether it is Sudoku
web安全--逻辑越权
Minecraft群組服開服
Tensorflow2 keras 分类模型
OpenFeign 簡單使用
Luogu greedy part of the backpack line segment covers the queue to receive water
OpenShift构建镜像
Web security -- Logical ultra vires
k8s入门:Helm 构建 MySQL