数组元素循环右移问题
来源:互联网 发布:绝地求生大逃杀优化差 编辑:程序博客网 时间:2024/06/09 19:44
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(
输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。
输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
代码
#include<stdio.h>int main(){ int N,M; int i,j; int a[101]; scanf("%d %d\n",&N,&M); for(i = 0; i < N; i++) { scanf("%d",&a[i]); } int temp1 = a[0]; for(i = 0;i < M;i++) { for(j = 0;j < N;j++) //give the later number to the former one,the last to the first { if(j == N-1) { a[0] = temp1; break; } int temp2 = a[j+1]; a[j+1] = temp1; temp1 = temp2; } } printf("%d",a[0]); for(i = 1;i < N;i++) { printf(" "); printf("%d",a[i]); //it is ok too: for(i = 0;i < N;i++) // printf("%d%s",a[i],(i-N+1?" ":"\n")); } return 0;}
阅读全文
0 0
- 数组元素循环左/右移问题
- 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- PAT1008:数组元素循环右移问题
- 数组元素循环左/右移问题
- 1008. 数组元素循环右移问题
- 1008 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 数组元素循环右移问题 (20)
- PAT数组元素循环右移问题
- pat数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 1008. 数组元素循环右移问题
- 数组元素循环右移问题
- PAT1008 数组元素循环右移问题
- 1008 数组元素循环右移问题
- PAT1008. 数组元素循环右移问题
- 1008.数组元素循环右移问题
- 例4.8 含有对象成员的派生类构造函数和析构函数的执行顺序
- Android比较优秀的开源项目
- 南阳ID:96 cigarettes
- 类多继承
- java.util.concurrent包图文源码解析(一)——CountDownLatch
- 数组元素循环右移问题
- x86,x64,Debug,release在VS中的编译如何选择
- 【JavaScript】—— 一键上传插件
- ubuntu下安装jenkins
- Hdu 1904 LCIS (dp)
- c++常对象和常成员函数详解
- 洛谷P1107 最大整数
- codeforces828C
- ubuntu 16.04安装opencv3.0流程以及报错解决