一个算法题目: 三角形

Posted funny_coding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个算法题目: 三角形相关的知识,希望对你有一定的参考价值。

 

输出结果

5 4 3 2 1 a
4 3 2 1 b
3 2 1 c
2 1 d
1 e

 

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        NumPrinter numPrinter = new NumPrinter();
        CharPrinter charPrinter = new CharPrinter();
        int charst = 97;
        for (int st = 5; st >= 1; st--) {
            numPrinter.print(st);
            charPrinter.print(charst++);
            System.out.println();
        }
    }

}

class NumPrinter {

    public void print(int i) {
        for (; i >= 1; i--) {
            System.out.print(i + " ");
        }
    }

}

class CharPrinter {
    public void print(int i) {
        System.out.print((char) i);
    }
}

 

关键是字符的输出,因为前面的数字是递减的,而字符是递增的,因此需要使用数字强转成char

 

以上是关于一个算法题目: 三角形的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个 CSS 片段可以画一个三角形? [复制]

⭐算法入门⭐《递推 - 二维》简单01 —— LeetCode 118. 杨辉三角

为啥保守光栅化无法为某些三角形调用片段着色器?

c++写算法题——杨辉三角

算法基础课题目时间

算法第3章上机实践报告