6-3 数组循环右移 (20分)
发布日期:2022-02-17 09:51:29
浏览次数:6
分类:技术文章
本文共 784 字,大约阅读时间需要 2 分钟。
6-3 数组循环右移 (20分)
本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>0)个整数,将每个整数循环向右移m(≥0)个位置,即将a中的数据由(a0a1 ⋯an−1)变换为(an−m⋯an−1 a0 a1⋯an−m−1 )(最后m个数循环移至最前面的m个位置)。
函数接口定义:
int ArrayShift( int a[], int n, int m );
其中a[]是用户传入的数组;n是数组的大小;m是右移的位数。函数ArrayShift须将循环右移后的数组仍然存在a[]中。裁判测试程序样例:
#include <stdio.h> #define MAXN 10int 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 4int ArrayShift( int a[], int n, int m ){ int i,j,temp; for(i=1;i<=m;i++){ temp=a[n-1]; for(j=n-1;j>0;j--){ a[j]=a[j-1]; } a[0]=temp; } }
转载地址:https://blog.csdn.net/qq_43828852/article/details/106498183 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月05日 16时48分08秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
crontab定时任务写法
2019-05-01
nginx: [emerg] unknown directive "if($remote_addr" in /usr/local/tools/nginx/conf/nginx.conf:57
2019-05-01
module pip has no attribute main问题解决
2019-05-01
LeetCode 130.Surrounded Regions (被围绕的区域)
2019-05-01
muduo网络库——详解muduo多线程模型
2019-05-01
muduo网络库源码分析——整体架构
2019-05-01
LeetCode 131.Palindrome Partitioning (分割回文串)
2019-05-01
LeetCode 134.Gas Station (加油站)
2019-05-01
LeetCode 135.Candy (分发糖果)
2019-05-01
为什么C++中stack的pop()函数不返回值而返回void
2019-05-01
LeetCode 141.Linked List Cycle (环形链表)
2019-05-01
LeetCode 143.Reorder List (重排链表)
2019-05-01
LeetCode 147.Insertion Sort List (对链表进行插入排序)
2019-05-01
python之字符串查找与替换
2019-05-01