首页 > 编程语言 > 详细

Java经典编程题50道之三十六

时间:2017-06-08 14:42:30      阅读:325      评论:0      收藏:0      [点我收藏+]

有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

public class Example36 {
    public static void main(String[] args) {
        int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };
        moveElement(m, 5);
    }

    public static void moveElement(int[] m, int n) {
        System.out.print("移位前的数组为:");
        for (int r : m) {
            System.out.print(r + " ");
        }

        if (n <= m.length) {
            int[] b = new int[m.length];
            for (int i = 0; i < m.length - n; i++) {
                b[i + n] = m[i];
            }
            int j = 0;
            for (int i = m.length - n; i < m.length; i++) {
                b[j] = m[i];
                j++;
            }

            System.out.print("\n移动" + n + "位后的数组为:");
            for (int r : b) {
                System.out.print(r + " ");
            }
        } else {
            System.out.print("\n移动错误!");
        }
    }
}

Java经典编程题50道之三十六

原文:http://www.cnblogs.com/qubo520/p/6962529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!