当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
统计字符串中各类字符的个数
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
Detailed explanation of NIN network
OpenFeign 简单使用
C language replaces spaces in strings with%20
Minecraft安装资源包
Service de groupe minecraft
Use the kaggle training model and download your own training model
Loadbalancer dynamically refreshes Nacos server
HCIA—應用層
什么是SQL注入
选择排序和插入排序
Chrome debugging
Sqli labs Level 2
路由基础—动态路由
HCIA - data link layer
NPOI 导出Word 字号对应
[blackmail virus data recovery] suffix Rook3 blackmail virus
Linux安装Oracle Database 19c