2021-06-10

7-1 数组循环左移 (20 分)
编程题

本题要求实现一个对数组进行循环左移的简单函数:一个数组a中存有n(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向左移m(≥0)个位置,即将a中的数据由
(a
​0
​​ a
​1
​​ ⋯a
​n−1
​​ )变换为(a
​m
​​ ⋯a
​n−1
​​ a
​0
​​ a
​1
​​ ⋯a
​m−1
​​ )

输入格式:
输入第1行给出正整数n(≤100)和整数m(≥0);第2行给出n个整数,其间以空格分隔。

输出格式:
在一行中输出循环左移m位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:
8 3
1 2 3 4 5 6 7 8
输出样例:
4 5 6 7 8 1 2 3
代码如下:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 101
typedef int ElemType;
typedef struct {
ElemType* elem;
int last;
}Sqlist;
void intsert(Sqlist& L)
{
L.elem = new ElemType[MAXSIZE];
L.last = 0;
}
void change(Sqlist& L, int n, int x)
{
int i, j, temp;
for (i = 0; i < x; i++)
{
temp = L.elem[0];
for (j = 1; j <= n - 1; j++)
{
L.elem[j - 1] = L.elem[j];
}
L.elem[n - 1] = temp;
}

}
int main()
{
Sqlist L;
long n, x, i;
scanf("%ld %ld", &n, &x);
intsert(L);
L.last = n;
x = x % n;
intsert(L);
for (i = 0; i < n; i++)
scanf("%d", &L.elem[i]);
change(L, n, x);
for (i = 0; i < n - 1; i++)
printf("%d “, L.elem[i]);
printf(”%d", L.elem[i]);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值