6-2 数组循环右移 (20 分)
 

本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>)个整数,将每个整数循环向右移m(≥)个位置,即将a中的数据由(a​0​​a​1​​⋯a​n−1​​)变换为(a​n−m​​⋯a​n−1​​a​0​​a​1​​⋯a​n−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

 1 int ArrayShift( int a[], int n, int m ){
2 int t=0;
3 for(int j=0;j<m;j++){
4 t=a[n-1];
5 for(int i=n-1;i>0;i--){
6
7 a[i]=a[i-1];
8 }
9 a[0]=t;
10 }
11 return 1;
12 }

PTA 数组循环右移的更多相关文章

  1. 线性表(一)——数组循环右移算法

    源码:rshift.cpp #include "stdafx.h" #include <stdio.h> /****************************** ...

  2. PTA自测-3 数组元素循环右移问题

    自测-3 数组元素循环右移问题  一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M≥0)个位置,即将A中的数据由(A0A1···A​N-1​​)变换为 ...

  3. PTA(BasicLevel)-1008数组元素循环右移问题

    一 .问题描述      原题描述 将长度为n的整形数组A进行右移m位操作, [A0 A1 A2 A3 ... Am...An-1]变为[An-m...An-1 A0 A1 A2 A3 ...An-m ...

  4. PTA(Basic Level)1008.数组元素循环右移问题

    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN−M−1)(最 ...

  5. PTA | 1008 数组元素循环右移问题 (20分)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...

  6. PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌

    -自测1. 打印沙漏() 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号 ...

  7. PAT 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...

  8. PAT乙级 1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  9. PAT (Basic Level) Practise:1008. 数组元素循环右移问题

    [题目连接] 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… A ...

随机推荐

  1. Bash on Ubuntu on Windows ( Windows Subsystem for Linux)

    1 #  Bash on ubuntu on Windows http://www.cnblogs.com/anonymous-ufo/p/6143480.html 1 1 如何启用Bash on u ...

  2. how to import a SQL file in MySQL command line

    how to import a SQL file in MySQL command line execute .sql file, macOS $mysql> source \home\user ...

  3. GPU 加速 & WebGL

    GPU 加速 & WebGL 开启 GPU 加速, 硬件加速 垃圾面试官,瞎忽悠 holy shit 美国想象力英语,前端 leader WebGL 加速 ??? 是什么鬼 ??? three ...

  4. computer network layers architecture (TCP/IP)

    computer network layers architecture (TCP/IP) 计算机网络分层架构 TCP/IP 协议簇 OSI 模型(7 层) TCP/IP (4 层) Applicat ...

  5. TypeScript & global.d.ts

    TypeScript & global.d.ts https://www.typescriptlang.org/docs/handbook/declaration-files/template ...

  6. layui 时间插件laydate中动态设置改变min和max值

    <div class="layui-inline"> <label class="layui-form-label">申请时间</ ...

  7. NGK.IO超级节点是我们掌握的下一个财富密码吗?

    从日前NGK.IO发布的新闻中,我们捕捉到了一个非常重要的信息,那就是反复被提到的"超级节点".很多人都对这个"超级节点"一头雾水,这个"超级节点&q ...

  8. django学习-23.admin管理后台的数据表数据的自定义展示

    目录结构 1.前言 2.自定义设置一张指定的数据表的列表展示内容 2.1.第一步:如果我们想让数据表[hello_person]里面的表字段值全部展示出来,需在应用[hello]里的[admin.py ...

  9. hadoop的lzo支持

    目录 1.下载相关文件: 2.Configure LZO to build a shared library (required) and use a package-specific prefix ...

  10. CentOS7 下Docker最新入门教程 超级详细 (安装以及简单的使用)

    转载https://blog.csdn.net/wzsy_ll/article/details/82866627 1.为什么使用Docker(本人) 最近总是频繁的在新服务器发布项目, 每次发布都需要 ...