当前位置:网站首页>Move a string of numbers backward in sequence
Move a string of numbers backward in sequence
2022-07-02 08:47:00 【Programmers on the road】
Move a series of numbers backward
One 、 Title Description
Yes n It's an integer , Move the front numbers back in order m A place , Last m The number becomes the first m Number (m<n).( More , Please see the Programmers are on the road )
Two 、 Analysis answers
This problem is mainly about the training of programming logic . It's about arrays 、 Knowledge points of pointer , This topic can also well express the choice of different data structures , There will be completely different ideas for solving the same problem , Programming complexity will also vary greatly .
Method 1 : use Array Data structures store data . Ideas :① Use a temporary array , Put the last... In the original array m First copy the numbers into the new array , Then move the elements in the original array backward m A place , Finally, copy the elements in the temporary array to the beginning of the original array .② Use a temporary array with the same length as the original array , After the original array m Numbers are copied to the front of the temporary array m position , Then copy the remaining numbers of the original array to the back of the temporary array , Then return the temporary array .
It is worth noting that the idea ①② The size of the temporary array space is different , There is a slight difference in programming . Also pay attention to the calculation of array subscripts , This is easy to make mistakes .
#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;
}Method 2 : use Single chain list Data structures store data . Ideas : Will be the first (N - M) The element of the position is the first node ( That's the end m The first element node of an element ), The first node element of the original linked list can be linked to the end node of the original linked list .
#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;
// Input N It's an integer
scanf("%d",&n);
while(i++<n){
// The memory space occupied by the application node
if( (p = (Node *)malloc(sizeof(Node))) == NULL){
printf("memery is not available");
exit(1);
}
scanf("%d", &(p->data));
p->next = NULL;
// The current application is for the first node
if(head == NULL){
head = tail = p;
}else{
// Tail interpolation
tail->next = p;
tail = p;
}
}
// Find No (N-M) Location of the precursor node .
scanf("%d",&m);
i=0;
p = head;
while( i++<n -m -1){
p = p->next;
}
// Notice the exchange of these elements . Is the core of realizing this topic
// Last m The first element of the elements acts as the chain head , The first node element of the original linked list is linked to the end node of the original linked list . In this way, the backward shift is realized
tail->next = head;
head = p->next;
tail = p;
tail->next = NULL;
// Output processed number
p = head;
while(p){
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return 0;
}Schematic diagram of program running results :

3、 ... and 、 Summary
From the above two different methods to solve the same problem, we can see , Choice of different data structures , It has a great impact on programming ideas , The ingenious choice of data structure sometimes greatly simplifies the understanding of programming logic . meanwhile , Different data structures will also make a great difference in the time complexity and space complexity of the program algorithm .
边栏推荐
- Qunhui NAS configuring iSCSI storage
- sqli-labs第12关
- File upload Labs
- OpenFeign 簡單使用
- Zipkin is easy to use
- Learning C
- ICMP Protocol
- sqli-labs第2关
- Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
- Qt——如何在QWidget中设置阴影效果
猜你喜欢

Web security -- Logical ultra vires

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel

Linux binary installation Oracle database 19C

Hcia - Application Layer

sqli-labs第1关

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
![[blackmail virus data recovery] suffix Hydra blackmail virus](/img/27/f44334cf98229d0f8b33c70a878ca8.jpg)
[blackmail virus data recovery] suffix Hydra blackmail virus

HCIA—数据链路层

Linux安装Oracle Database 19c
![DWORD ptr[]](/img/6e/f68863c9f5b8608b22a24d9c1836d9.jpg)
DWORD ptr[]
随机推荐
Openfeign facile à utiliser
Network security - summary and thinking of easy-to-use fuzzy tester
Googlenet network explanation and model building
Benefits of ufcs of D
Gateway is easy to use
commands out of sync. did you run multiple statements at once
Tensorflow2 keras classification model
zipkin 简单使用
图像变换,转置
Minecraft安装资源包
Web security -- core defense mechanism
统计字符串中各类字符的个数
sqli-labs第2关
Installing Oracle database 19C RAC on Linux
Application of kotlin - higher order function
一个经典约瑟夫问题的分析与解答
Shortcut key to comment code and cancel code in idea
OpenShift构建镜像
HCIA - data link layer
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it