当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
Tensorflow2 keras classification model
Zipkin is easy to use
Qt的拖动事件
Viewing JS array through V8
commands out of sync. did you run multiple statements at once
OpenShift 容器平台社区版 OKD 4.10.0部署
Gateway is easy to use
web安全--逻辑越权
D interface and domain problems
PCL calculates the intersection of three mutually nonparallel planes
Web技术发展史
Npoi export word font size correspondence
Call Stack
Use Wireshark to grab TCP three handshakes
File upload Labs
一个经典约瑟夫问题的分析与解答
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
文件上传-upload-labs
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Sqli labs level 12