#define _CRT_SECURE_NO_WARNINGS
/*已知在一维数组A[m+n]中依次存放两个线性表(a1,a2,a3,...am)和(b1,b2,b3,...bn)。编写一个函数,
将数组中两个顺序表的位置互换,即(b1,b2,b3,...bn)放在(a1,a2,a3,...am)的前面*/
//算法思想:先逆置(1 2 3 4 5 6 7变为7 6 5 4 3 2 1),然后将前n个再逆置(假如n=4,则前n个逆置为4 5 6 7),
//将后m个逆置(假如m=3,则后m个逆置为1 2 3)
#include<stdio.h>
#define MaxSize 50
struct SqList {
int a[MaxSize] = {1,2,3,4,5,6,7};
int length = 7;
};
// 逆置操作
void Reverse(SqList&L,int left,int right)
{
for (int i= left; i<= (left + right) / 2; i++)
{
int t = L.a[i];
L.a[i] = L.a[left + right - i];
L.a[left + right - i] = t;
}
}
void Print(SqList L)
{
for (int i = 0; i < L.length; i++)
{
printf("%d ", L.a[i]);
}
printf("\n");
}
void Change(SqList& L, int m, int n)
{
printf("整个逆置为:");
Reverse(L, 0, m + n - 1);
Print(L);
printf("前n个逆置为:");
Reverse(L, 0, n - 1);
Print(L);
printf("后m个逆置为:");
Reverse(L, n, m + n - 1);
Print(L);
}
int main()
{
SqList L;
Change(L, 3, 4);//m 1 2 3 n 4 5 6 7
return 0;
}
25王道代码题2_2_07
最新推荐文章于 2024-11-01 14:57:44 发布