每日作业20200619 - 矩形行交换、矩形旋转

题目

基础版
    矩形行交换
    输入5*5的矩形 和两个整数n,m(1<= n, m <= 5)
    将矩形的第n行和第m行交换, 并打印
        样例输入:
            1 2 2 1 2
            5 6 7 8 3
            9 3 0 5 3
            7 2 1 4 6
            3 0 8 2 4
            1 5
        样例输出:
            3 0 8 2 4
            5 6 7 8 3
            9 3 0 5 3
            7 2 1 4 6
            1 2 2 1 2
    --------------------------------
进阶版
	矩形变换
	输入5*5的矩形A 以及三个整数x, y, a (x + a <= 6,   y + a <= 6)
	矩形A中的小矩形B左上角位置在第x列, 第y行. 大小为a*a
	将小矩形B顺时针旋转90°后打印
	    样例输入:
	        1 2 2 1 2
	        5 6 7 8 3
	        9 3 0 5 3
	        7 2 1 4 6
	        3 0 8 2 4
	        1 3 3
	    样例输出:
	        1 2 2 1 2
	        5 6 7 8 3
	        3 7 9 5 3
	        0 2 3 4 6
	        8 1 0 2 4

代码

public class Homework0619 {
    public static void main(String[] args) {
        Scanner sc = null;
        int num = 0;
        while (true){
            System.out.println("\n************************************************************");
            System.out.println("1、标准作业\t\t2、进阶作业\t\t3、退出");
            System.out.print("请输入对应编号,查看对应答案: ");
            sc = new Scanner(System.in);
            num = sc.nextInt(); //接收用户输入

            if (num == 1){
                System.out.println("\n******************标准作业******************");
                basics();   //调用方法
            }else if (num == 2){
                System.out.println("\n******************进阶作业******************");
                advanced(); //调用方法
            }else if (num == 3){
                System.out.println("\n******************系统退出******************");
                break;
            }
            else{
                System.out.println("输入错误,请重新输入");
            }
        }
    }

//**********************************************************************************************************************************
    /**
     * 方法--进阶作业
     */
    public static void advanced() {
        String[][] arrayA = inputArray();       //调用输入方法,得到合法的数组
        int length = arrayA[0].length;          //数组长度
        int[] para = para(3, length);     //调用方法,得到参数
        /*打印输入的数组与参数*/
        System.out.println("输入的数组为");
        print(arrayA);
        System.out.println("输入的参数为");
        print(para);    //1,3,3

        System.out.println("\n得到 arrayB");
        String[][] arrayB = getArrayB(arrayA, para);
        print(arrayB);

        System.out.println("\n******将数组 arrayB 旋转得到******");
        arrayB = rotate(arrayB);
        print(arrayB);

        for (int i = 0; i < para[2]; i++){
            System.arraycopy(arrayB[i], 0, arrayA[para[1] + i - 1], para[0] - 1, para[2]);     //将 旋转完的 arrayB 放进 arrayA 中
        }

        System.out.println("\n******新数组 arrayA 旋转得到******");
        print(arrayA);

    }

    /**
     * 从输入的数组中,得到小数组
     * @param arrayA    输入的数组
     * @param para  参数
     * @return  返回小数组
     */
    public static String[][] getArrayB(String[][] arrayA, int[] para) {
        String[][] arrayB = new String[para[2]][para[2]];
        for (int i = 0; i < para[2]; i++){
            System.arraycopy(arrayA[para[1] + i - 1], para[0] - 1, arrayB[i], 0, para[2]);     //对 arrayB 赋值
        }
        return arrayB;
    }

    /**
     * 旋转
     * @param arrayB 小数组
     */
    public static String[][] rotate(String[][] arrayB) {
        int count = arrayB.length;      //长度
        String[][] change = new String[count][count];   //定义旋转后的数组
        String[] temp = new String[count];  //定义临时数组
        for (int i = 0; i < count; i++){
            for (int j = 0; j < count; j++) {
                temp[j] = arrayB[count - 1 - j][i];  //取出第j列的元素,并倒序
            }

            for (int j = 0; j < temp.length; j++){
                change[i][j] = temp[j];     //将temp数组放到output数组的第i行中
            }
        }
        return change;
    }
//**********************************************************************************************************************************
    /**
     * 方法--标准作业
     */
    public static void basics() {
        String[][] array = inputArray();     //调用输入方法,得到合法的数组
        int length = array[0].length;   //数组长度
        int[] para = para(2, length);     //调用方法,得到参数

        System.out.println("输入的数组为");
        print(array);
        System.out.println("输入的参数为");
        print(para);

        System.out.println("\n******结果******");

        String[] temp = new String[length];     //临时数组
        System.arraycopy(array[para[0] - 1], 0, temp, 0, length);     //将 array[para[0] - 1] 复制给 temp
        System.arraycopy(array[para[1] - 1], 0, array[para[0] - 1], 0, length);     //将 array[para[1] - 1] 复制给 array[para[0]]
        System.arraycopy(temp, 0, array[para[1] - 1], 0, length);     //将 temp 复制给 array[para[1] - 1]
        print(array);

    }

//**********************************************************************************************************************************
    /**
     * 打印数组
     * @param str
     */
    public static void print(int[] str) {
        for(int i = 0; i < str.length; i++) {
            System.out.print(str[i] + "\t");
        }
        System.out.println();
    }

    public static void print(String[][] str) {
        for(int i = 0; i < str.length; i++){
            for (int j = 0 ; j < str[i].length; j++){
                System.out.print(str[i][j] + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 是否继续计算
     */
    public static boolean judge() {
        System.out.print("\n是否继续?输入y继续计算,其他则退出: ");
        Scanner sc = new Scanner(System.in);
        if ("y".equalsIgnoreCase(sc.next())){
            System.out.print("继续判断,");
            return true;
        }else{
            System.out.println("退出计算!");
            return false;
        }
    }

    /**
     * 输入数组
     * @return 合法的数字
     */
    public static String[][] inputArray() {
        Scanner sc = new Scanner(System.in);
        System.out.println("\n***输入 n*n 的数组***\n");

        System.out.print("请输入第1行(用空格隔开):");
        String str1 = sc.nextLine();   //接收用户输入
        String[] input1 = str1.trim().split("\\s+");  //去掉首尾得空格,将有效字符串以空格为分隔符存放进数组
        int count = input1.length;      //用户输入的个数

        String str_temp = null; //临时字符串
        String[] input_temp = new String[count];    //临时数组
        String[][] input = new String[count][count];    //二维数组,存放最终结果,并输出
        input[0] = input1;  //将第一行的输入放进二维数组中的第一个

        for(int i = 1; i < count; i++){     //循环,确保输入的是个正方形矩阵
            while (true){
                System.out.print("请输入第" + (i + 1) + "行(用空格隔开):");
                str_temp = sc.nextLine();   //接收用户输入
                input_temp = str_temp.trim().split("\\s+");  //去掉首尾得空格,将有效字符串以空格为分隔符存放进数组
                if(input_temp.length == count){
                    input[i] = input_temp;    //将结果放进数组中
                    break;
                }else{
                    System.out.println("**请输入" + count + "个字符**");
                }
            }
        }
        return input;
    }

    /**
     * 输入参数
     * @return 合法的数字
     */
    public static int[] para(int num, int length) {
        System.out.println("\n***请输入参数***\n");
        int[] para = new int[num];  //定义参数数组
        int temp = 0;   //临时变量

        if (num == 2){
            System.out.println("请依次输入2个参数,表示需要交换的行");
        }else {
            System.out.println("请依次输入3个参数,分别表示小矩形B左上角位置在矩形A中的第x列, 第y行. 大小为a*a");
        }

        for (int i = 0; i < 2; i++){
            System.out.print("请输入第 " + (i + 1) + " 个参数:" );
            para[i] = input(length);        //用户输入
        }

        if (num == 3){
            System.out.print("请输入第 3 个参数:" );
            int count = 0;
            while(true){
                count = input(length);  //输入第三个参数
                if (para[0] + count <= 6 && para[1] + count <= 6){  //判断
                    para[2] = count;
                    break;
                }else{
                    System.out.println("第3个参数分别与前两个参数的和应不大于 " + (length + 1) );
                    continue;
                }
            }
        }
        System.out.println();
        return para;
    }

    /**
     * 输入方法
     * @return 合法的数字
     */
    public static int input(int length) {
        Scanner sc = null;
        int num = 0;    //存放数字
        boolean flag = false;   //判断是否有输入下一个数字
        while(true){
            sc = new Scanner(System.in);
            flag = sc.hasNextInt();
            if (flag){      //输入的是整数
                num = sc.nextInt();    //存放输入的数字
                if (num > 0 && num <= length){
                    return num;     //如果是正整数,则return
                }else{
                    System.out.print("输入错误,请输入一个 1~" + length + " 的数: ");
                }
            }else{
                System.out.print("输入错误,请输入一个整数: ");
            }
        }
    }

}

运行结果

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 1

******************标准作业******************

***输入 n*n 的数组***

请输入第1(用空格隔开)11 12 13 14 15
请输入第2(用空格隔开)21 22 23 24 25
请输入第3(用空格隔开)31 32 33 34 35
请输入第4(用空格隔开)41 42 43 44 45
请输入第5(用空格隔开)51 52 53 54 55

***请输入参数***

请依次输入2个参数,表示需要交换的行
请输入第 1 个参数:1
请输入第 2 个参数:5

输入的数组为
11	12	13	14	15	
21	22	23	24	25	
31	32	33	34	35	
41	42	43	44	45	
51	52	53	54	55	
输入的参数为
1	5	

******结果******
51	52	53	54	55	
21	22	23	24	25	
31	32	33	34	35	
41	42	43	44	45	
11	12	13	14	15	

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 2

******************进阶作业******************

***输入 n*n 的数组***

请输入第1(用空格隔开)11	12	13	14	15	
请输入第2(用空格隔开)21	22	23	24	25	
请输入第3(用空格隔开)31	32	33	34	35
请输入第4(用空格隔开)41	42	43	44	45	
请输入第5(用空格隔开)51	52	53	54	55

***请输入参数***

请依次输入3个参数,分别表示小矩形B左上角位置在矩形A中的第x列, 第y行. 大小为a*a
请输入第 1 个参数:1
请输入第 2 个参数:3
请输入第 3 个参数:3

输入的数组为
11	12	13	14	15	
21	22	23	24	25	
31	32	33	34	35	
41	42	43	44	45	
51	52	53	54	55	
输入的参数为
1	3	3	

得到 arrayB
31	32	33	
41	42	43	
51	52	53	

******将数组 arrayB 旋转得到******
51	41	31	
52	42	32	
53	43	33	

******新数组 arrayA 旋转得到******
11	12	13	14	15	
21	22	23	24	25	
51	41	31	34	35	
52	42	32	44	45	
53	43	33	54	55	

************************************************************
1、标准作业		2、进阶作业		3、退出
请输入对应编号,查看对应答案: 3

******************系统退出******************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值