题解 | #圣诞树#

圣诞树

http://www.nowcoder.com/practice/9a03096ed8ab449e9b10b0466de29eb2

import java.util.Scanner;

// 1. 比较笨,弄的挺长时间的
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int row = n * 3;
            int count = 0;
            for (int i = 0; i < row; i++) {
                // 输出空格
                for (int j = 0; j < row - 1 - i; j++) {
                    System.out.print(" ");
                }
                // 输出内容
                for (int j = 0; j <= i % 3; j++) {
                    System.out.print("* ");
                }
                // 因为一开始每一个右边的*和左边的都是4个空格,所以这里产生一个变量,以便输出空格
                // 第二个就是 2个
                // 第三就是 0
               //  为了循环,加一个对3的判断,初始化变量
                if (i % 3 == 0) {
                    count = 0;
                }
                // 输出右边的值
                for (int k = 1; k <= i / 3; k++) {
                    // 输出空格
                    for (int j = 0; j < 4 - count; j++) {
                        System.out.print(" ");
                    }
                    // 输出内容
                    for (int j = 0; j <= i % 3; j++) {
                        System.out.print("* ");
                    }
                }
                count += 2;
                System.out.println();
            }
            // 输出
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < row - 1; j++) {
                    System.out.print(" ");
                }
                System.out.println("*");
            }
        }
    }
}
全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
大飞的诡术妖姬:之前看b站多明海有个说法,日本就业竞争非常低的原因不光是毕业学生少,还有很多人干两年不喜欢职场氛围就辞职躺平,位置也空了很多,论吃苦耐劳还得看咱们
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务