题目:
解题思路:
?
代码:
#include<stdio.h> #include<stdlib.h> #define N 100 typedef struct SList { ?? ?int data[N]; ?? ?int size; }SL; void SLInit(SL* ps) { ?? ?ps = (SL*)malloc(sizeof(SL)); ?? ?ps->size = 0; } void SLCreate(SL* ps,int n) { ?? ?int x = 0; ?? ?ps->size = n;//这一步一定不能丢 ?? ?for (int i = 0; i < ps->size; i++) ?? ?{ ?? ??? ?scanf("%d", &x); ?? ??? ?getchar(); ?? ??? ?ps->data[i] = x; ?? ?} } void SLReverse(SL* ps, int begin, int end) { ?? ?while (begin <= end) ?? ?{ ?? ??? ?int tmp = ps->data[begin]; ?? ??? ?ps->data[begin] = ps->data[end]; ?? ??? ?ps->data[end] = tmp;
?? ??? ?begin++; ?? ??? ?end--; ?? ?} } void SLMove(SL* ps, int p) { ?? ?SLReverse(ps, 0, p - 1); ?? ?SLReverse(ps, p, ps->size - 1); ?? ?SLReverse(ps, 0, ps->size - 1); } void SLPrint(SL* ps) { ?? ?for (int i = 0; i < ps->size; i++) ?? ?{ ?? ??? ?printf("%d ", ps->data[i]); ?? ?} } int main() { ?? ?SL plist; ?? ?SL* pl = &plist; ?? ?SLInit(pl); ?? ?int n = 0; ?? ?scanf("%d", &n); ?? ?getchar(); ?? ?SLCreate(pl, n); ?? ?int p = 0; ?? ?scanf("%d", &p); ?? ?if (p <= 0 || p >= n) ?? ??? ?printf("error!"); ?? ?else ?? ?{ ?? ??? ?SLMove(pl, p); ?? ??? ?SLPrint(pl); ?? ?} ?? ?return 0; }
?
|