1008. 数组元素循环右移问题 (20)
2016-04-30 11:01
791 查看
</pre>一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A<sub style="line-height:0">0</sub>A<sub style="line-height:0">1</sub>……A<sub style="line-height:0">N-1</sub>)变换为(A<sub style="line-height:0">N-M</sub> …… A<sub style="line-height:0">N-1</sub> A<sub style="line-height:0">0</sub> A<sub style="line-height:0">1</sub>……A<sub style="line-height:0">N-M-1</sub>)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?<p></p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; line-height:18px; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)"><strong>输入格式:</strong>每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。</p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; line-height:18px; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)"><strong>输出格式:</strong>在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。</p><span style="color:rgb(51,51,51); font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; line-height:18px; background-color:rgb(250,250,250)">输入样例:</span><pre style="margin-top:1.5em; margin-bottom:1.5em; padding:0px; border:0px; font-family:'Droid Sans Mono',Consolas,'Courier New',monospace; line-height:18px; vertical-align:baseline; overflow:auto; color:rgb(51,51,51); background-color:rgb(250,250,250)">6 2 1 2 3 4 5 6输出样例:
5 6 1 2 3 4
代码如下:
#include<stdio.h> main() { int m[200]; int n,wm,/**p*/i; scanf("%d",&n); scanf("%d",&wm); for(i=0;i<n;i++) { scanf("%d",&m[i]); } wm=wm%n; /* for(i=0;i<n-wm;i++); { p++; }*/ for(i=n-wm;i<n;i++) { printf("%d ",m[i]); } for(i=0;i<n-wm;i++) { if(i==(n-wm-1)) { printf("%d",m[i]); } else printf("%d ",m[i]); } }
相关文章推荐
- 启动Tomcat 失败(Unable to start cluster)及解决方法
- 深入浅出ES6(十一):生成器 Generators,续篇
- 设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型)
- Codeforces Round #349 (Div. 2)
- Reboot/halt/shutdown command on CentOS 7
- Code Hunt 题解 05-08 (Java)
- Linux三个特殊权限 setuid setgid stick bit
- FTP连接报错530 Permission denied解决方法
- C语言中的atoi函数的实现
- [C++]详解异常处理(Exception Handling) 及标准库异常处理类
- 最短路径条数问题
- PHP经典算法计算两个日期的差数
- iOS layout的相关方法
- Hibernate 延迟加载剖析与代理模式应用
- Linux下整合Apache和Tomcat
- fprintf(); fscanf(); 向文件中写入读出数据
- opencv中的cvResize函数,cvSize结构体,IplImage结构体
- 设计模式(十一)代理模式Proxy(结构型)
- HDU 3388 Coprime(容斥原理+二分)
- 深入浅出ES6(十):集合