题解 | #超级圣诞树#

超级圣诞树

https://www.nowcoder.com/practice/470d26c9a73e4e17be8cc45cac843423

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 2;
        /*定义一个基础三角*/
        int [][]fir = {{0, 0, 1, 0, 0}, {0, 1, 0, 1, 0}, {1, 0, 1, 0, 1}};
	 	/*定义一个最终要打印的三角*/
        int [][]fina = new int[3 * (int)Math.pow(2, n)][3 * (int)Math.pow(2,
                n) - 1]; 
	    /*如果n为1,直接打印初始三角*/
        if (n == 1) {
            for (int[] ints : fir) {
                for (int anInt : ints) {
                    if (anInt == 1) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
	  
	    /*n不为1,进入循环*/
        while (count <= n) {
            /*将上三角向下平移*/
            int [][]sec = new int[3 * (int)Math.pow(2, count - 1)][3 * (int)Math.pow(2,
                    count) - 1];
            for (int i = 0; i < sec.length / 2; i++) {
                for (int j = 0; j < sec[i].length / 2; j++) {
                    sec[i][j] = fir[i][j];
                    sec[i + 3 * (int)Math.pow(2, count - 1) / 2][j] = fir[i][j];
                }
            }
            /*将下三角向右平移*/
            for (int i = sec.length / 2; i < sec.length; i++) {
                for (int j = 0; j < sec[i].length / 2; j++) {
                    sec[i][j + sec[i].length / 2 + 1] = sec[i][j];
                }
            }
            /*抹除上三角*/
            for (int i = 0; i < sec.length / 2; i++) {
                for (int j = 0; j < sec[i].length / 2; j++) {
                    sec[i][j] = 0;
                }
            }
            /*重写上三角*/
            for (int i = sec.length / 2; i < sec.length; i++) {
                for (int j = 0; j < sec[i].length / 2; j++) {
                    sec[i - sec.length / 2][j + sec[i].length / 4 + 1] = sec[i][j];
                }
            }
			/*将本次循环得到的图像,作为下一次的基础图形*/
            fir = sec;
		  
		    /*如果是最后一次循环,则保存本次结果*/
            if (count == n) {
                fina = sec;
            }
            count++;
        }
	    /*打印n不为1时的顶部三角图形*/
        if (n != 1) {
            for (int[] x : fina) {
                for (int y : x) {
                    if (y == 0) {
                        System.out.print(" ");
                    } else {
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
        }
        /*打印树干*/
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < fina[i].length; j++) {
                if (j == fina[i].length / 2) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

全部评论

相关推荐

挣K存W养DOG:我记得好多人说这个公司就是白嫖方案的,现在有大体方案要让你给他展示实现细节了,也是无敌了
点赞 评论 收藏
分享
04-02 16:49
门头沟学院 Java
_bloodstream_:我也面了科大讯飞,主管面的时候听说急招人优先考虑能尽快实习的,我说忙毕设,后面就一直没消息了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务