代码如下:
代码思想:
(ab)->(ba)
;
#include <iostream>
#include <stdio.h>
#define Maxsize 10
typedef struct Array{
int length;
int elem[Maxsize];
}Array;
void Move(Array &A, int step){
int a = step;
int b = A.length - step;
void print(Array &A);
void Upside_down(Array &A, int length, int first);
Upside_down(A, a,1);//对片段a进行倒序;
print(A);
Upside_down(A, b,a+1);//对片段b进行倒序;
print(A);
Upside_down(A, A.length,1);//对整个数组进行倒序
};
void Upside_down(Array &A, int length,int first){//倒序函数
int key = 0;
for (int i = 0; i < (length / 2); i++){
key = A.elem[first-1+i];
A.elem[first-1+i] = A.elem[first+length-2-i];
A.elem[first + length - 2 - i] = key;
}
};
void print(Array &A){//输出函数;
for (int j = 0; j < A.length; j++){
printf_s("%d", A.elem[j]);
}
printf_s("\n");
};
int main(){//主函数
Array A = { 7, { 1, 2, 3, 4, 5, 6, 7 } };
void print(Array &A);
void Move(Array &A, int step);
Move(A, 2);
print(A);
system("pause");
return 0;
system("pause");
}