00-自测3. 数组元素循环右移问题 (20)
2015-04-07 15:08
471 查看
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。
输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
输出样例:
输入格式:每个输入包含一个测试用例,第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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static int N; static int M; static int[] A; public static void main(String[] args) throws IOException { // System.out.println("..."); BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); String line=buf.readLine(); String[] temp; temp=line.split(" "); N=Integer.parseInt(temp[0]); M=Integer.parseInt(temp[1]); M=M%N; line=buf.readLine(); temp=line.split(" "); A=new int[temp.length]; boolean[] a=new boolean[temp.length]; for(int i=0;i<A.length;i++) { A[i]=Integer.parseInt(temp[i]); a[i]=false; } // System.out.println("..."); // System.out.println(A.length); if(M==0) ; else { for(int i=0;i<M;i++) { int t1,t2; t1=A[i]; for(int j=i;a[(j+M)%a.length]==false;j+=M) { t2=A[(j+M)%A.length]; A[(j+M)%A.length]=t1; t1=t2; a[(j+M)%a.length]=true; } } } for(int i=0;i<A.length-1;i++) System.out.print(A[i]+" "); System.out.println(A[A.length-1]); } }
相关文章推荐
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测3-数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- PAT-中国大学MOOC-陈越、何钦铭-数据结构2015春习题集-00-自测3. 数组元素循环右移问题 (20) 【二星级】
- 数据结构练习 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20) java实现
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 自测-3 数组元素循环右移问题(20 分)
- 00-自测3. 数组元素循环右移问题
- 00-自测3. 数组元素循环右移问题
- 1008. 数组元素循环右移问题 (20)
- 1008. 数组元素循环右移问题 (20)
- PAT1008 BASIC:数组元素循环右移问题 (20)
- 1008. 数组元素循环右移问题 (20)