Two linear table exchange positions (array elements or circular movement)

Into two tables in a linear array, one after the other, two linear table switching positions. As used herein, a linear table, the first n elements representing the first linear table, the m elements representing the second linear table.

The elements in the array corresponds to the n-th loop left or right of m.

Code:

#include <stdio.h>
#define MAX 100
struct Sqlist{
    int data[MAX];
    int length;
}s;
void input(Sqlist *s) {
    printf("请输入元素个数:");
    scanf("%d",&s -> length);
    printf("请输入%d个元素:",s -> length);
    for(int i = 0;i < s -> length;i ++) {
        scanf("%d",&s -> data[i]);
    }
}
void print(Sqlist s) {
    printf("%d\n",s.length);
    for(int i = 0;i < s.length;i ++) {
        printf("%d ",s.data[i]);
    }
    putchar('\n');
}
void reverse_s(Sqlist *s,int l,int r) {
    while(l < r) {
        s -> data[l] ^= s -> data[r];
        s -> data[r] ^= s -> data[l];
        s -> data[l] ^= s -> data[r];
        l ++;
        r --;
    }
}
void swap_s(Sqlist *s,int n,int m) {
    reverse_s(s,0,n + m - 1);
    reverse_s(s,0,m - 1);
    reverse_s(s,m,n + m - 1);
}
int main() {
    input(&s);
    int n,m;
    printf("请输入n,m:");
    scanf("%d%d",&n,&m);
    swap_s(&s,n,m);
    print(s);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/8023spz/p/11838444.html