There are n integers in the title description , so the previous numbers are moved backward m positions, and the last m numbers become the previous m numbers. Write a function: To achieve the above function, input n numbers and output adjusted n numbers in the main function.
Input
the number of input data nn integers to move the position m
output
after the move n number
样例输入
10
1 2 3 4 5 6 7 8 9 10
2
样例输出
9 10 1 2 3 4 5 6 7 8
Problem-solving ideas
First write an inverse function to exchange data in a given range
and then pass a total inverse function. First reverse the number before m, then reverse the number after m, and finally convert the whole Array transfer. Move back operation is complete.
#include <stdio.h>
//接收用户数据
void input_data(int* num, int n);
//逆置数组
void Reverse(int* num, int q, int p);
//总逆置
void convert(int* num, int n);
//输出数据
void put_data(int* num, int n);
int main(void){
int num[20];
//接收用户给定的数组大小
int n;
scanf("%d", &n);
//调用函数实现功能
input_data(num, n);
convert(num, n);
put_data(num, n);
return 0;
}
//接收用户数据
void input_data(int* num, int n){
//函数需要两个参数:*num 用来初始化的数组,n 数组的长度
//接收用户数据
for(int i = 0; i < n; i++)
scanf("%d", &num[i]);
}
//逆置数组
void Reverse(int* num, int q, int p){
int temp;
//对指定范围内的数据进行互换
for(; q < p; q++, p--){
temp = num[q];
num[q] = num[p];
num[p] = temp;
}
}
//总逆置
void convert(int* num, int n){
//接收数据要后移的位置
int m;
scanf("%d", &m);
//判断操作数据合法性
if(m <= 0 || m >= n) return;
else{
//下面有操作不同范围时,数组内的数据详情
Reverse(num, 0, n - m - 1);
Reverse(num, n - m, n - 1);
Reverse(num, 0, n - 1);
}
}
//输出数据
void put_data(int* num, int n){
for(int i = 0; i < n; i++){
printf("%d ", num[i]);
}
}
Update progress this month
Creation is not easy, your likes are my biggest motivation! ! !
See you next time end~