java,写题目时总是超时或超运存,想请教大佬有没有一些好的技巧
查看原帖
java,写题目时总是超时或超运存,想请教大佬有没有一些好的技巧
1225971
vacilating楼主2024/5/1 09:51
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        short n = in.nextShort();
        int len = (int) Math.pow(2,n);
        char[][] matrix = new char[len][len];
        for (short i = 0; i < len; i++) {
            for (short j = 0; j < len; j++) {
                matrix[i][j] = '1';
            }
        }
        m1(matrix,len, (short) 0, (short) 0);
        for (short i = 0; i < len; i++) {
            for (short j = 0; j < len; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void m1(char[][] matrix,int len,short x,short y) {
        len /= 2;
        for (short i = x,i1 = 0; i1 < len; i++,i1++) {
            for (short j = y,j1 = 0; j1 < len; j++,j1++) {
                matrix[i][j] = '0';
            }
        }

        if(len != 0){
            m1(matrix,len,x, (short) (y+len));
            m1(matrix,len, (short) (x+len),y);
            m1(matrix,len, (short) (x+len), (short) (y+len));
        }
    }
}
2024/5/1 09:51
加载中...