2 Star 6 Fork 3

Cyouagain / C语言经典编程详解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
【C语言经典编程】习题8-3 数组循环右移 (20分).md 1.84 KB
一键复制 编辑 原始数据 按行查看 历史

微信搜索公众号【IT学长】:

习题8-3 数组循环右移

本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>0)个整数,将每个整数循环向右移m(≥0)个位置,即将a中的数据由(a​0 ​​ a1⋯a​n−1)变换为( a​n−m⋯a​n−1a0a1⋯an−m−1 )(最后m个数循环移最前面的m个位置)。

函数接口定义:

int ArrayShift( int a[], int n, int m );

其中 a[] 是用户传入的数组;n是数组的大小;m是右移的位数。函数 ArrayShift 须将循环右移后的数组仍然存在a[]中。

裁判测试程序样例:

#include <stdio.h>
#define MAXN 10

int ArrayShift( int a[], int n, int m );

int main()
{
    int a[MAXN], n, m;
    int i;

    scanf("%d %d", &n, &m);
    for ( i = 0; i < n; i++ ) scanf("%d", &a[i]);

    ArrayShift(a, n, m);

    for ( i = 0; i < n; i++ ) {
        if (i != 0) printf(" ");
        printf("%d", a[i]);
    }
    printf("\n");

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

6 2

1 2 3 4 5 6

输出样例:

5 6 1 2 3 4

代码:

int ArrayShift( int a[], int n, int m )
{
  int i;
  int k;
  for(k=1;k<=m;k++)
  {
    int temp=a[n-1];
    for(i=n-1;i>0;i--)
     {
        a[i]=a[i-1];
     }
     a[0]=temp;
  }
}
C
1
https://gitee.com/cyouagain/C_pta.git
git@gitee.com:cyouagain/C_pta.git
cyouagain
C_pta
C语言经典编程详解
master

搜索帮助