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

来源:互联网 发布:抓娃娃机技巧知乎 编辑:程序博客网 时间:2024/06/11 00:26
</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 21 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]);}elseprintf("%d ",m[i]);}}


0 0
原创粉丝点击