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

来源:互联网 发布:南天软件 待遇 编辑:程序博客网 时间:2024/06/12 19:36

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

一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。

输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

import java.util.Scanner;public class no1008{    public static void main(String[] args){        Scanner scanner =new Scanner(System.in);        int sum = scanner.nextInt();        int reflect_sum =scanner.nextInt();        scanner.nextLine();        String input =scanner.nextLine();        String[] input_all =input.split("\\s");        for(int x =sum-reflect_sum;x<sum;x++){            System.out.print(input_all[x]+" ");        }        for(int x=0;x<sum-reflect_sum-1;x++){            System.out.print(input_all[x]+" ");        }        System.out.print(input_all[sum-reflect_sum-1]);    }}

原创粉丝点击