该代码的时间复杂度是多少(Sierpinski)

时间:2019-10-15 11:25:48

标签: time-complexity

我很难计算这段代码的时间复杂度。

应该是 T(n)= 3T(?)+ 1 。 我要插入 n-1 吗?

public static void main(String[] args) {
    int level = console.nextInt();

    DrawingPanel p = new DrawingPanel(SIZE, SIZE);
    p.setBackground(Color.CYAN);
    Graphics g = p.getGraphics();

    int triangleHeight = (int) Math.round(SIZE * Math.sqrt(3.0) / 2.0);
    Point p1 = new Point(0, triangleHeight);
    Point p2 = new Point(SIZE / 2, 0);
    Point p3 = new Point(SIZE, triangleHeight);
    drawFigure(level, g, p1, p2, p3);
}

public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) {
    if (level == 1) 
        Polygon p = new Polygon();
        p.addPoint(p1.x, p1.y);
        p.addPoint(p2.x, p2.y);
        p.addPoint(p3.x, p3.y);
        g.fillPolygon(p);
    } else {
        Point p4 = midpoint(p1, p2);
        Point p5 = midpoint(p2, p3);
        Point p6 = midpoint(p1, p3);

        drawFigure(level - 1, g, p1, p4, p6);
        drawFigure(level - 1, g, p4, p2, p5);
        drawFigure(level - 1, g, p6, p5, p3);
    }
}

0 个答案:

没有答案