djmann1   4년 전


import java.util.*;
class Main
{
    public static void main(String[] args)
    {
        int n=(new Scanner(System.in)).nextInt();
        int spaceN=n;//별전 스페이스
        int pow=0;
        int len=0;//중간 스페이스길이
        StringBuilder[] star = new StringBuilder[n+1];
        star[1]=new StringBuilder("*");star[2]=new StringBuilder("* *");star[3]=new StringBuilder("*****");
        int[] pow2= new int[11];
        for(int i=0;i<11;i++)
            pow2[i]=(int)Math.pow(2,i)*3;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=spaceN;j++)
                System.out.print(" ");
            if(i<=3)
            {
                System.out.print(star[i]);
                if(i==3)
                    len=5;
            }
            else if(i>pow2[pow])
            {
                System.out.print(star[i-pow2[pow]]);
                star[i]=new StringBuilder((star[i-pow2[pow]]));
                for(int l=0;l<len;l++)
                {
                    System.out.print(" ");
                    star[i].append(" ");
                }
                System.out.print(star[i-pow2[pow]]);
                star[i].append(star[i-pow2[pow]]);
                if(i==pow2[pow+1])
                {
                    pow++;
                    len=star[i].length();
                }
                else
                    len-=2;        
            }
            for(int j=1;j<=spaceN;j++)
                System.out.print(" ");
            spaceN--;
            if(i!=n)
                System.out.println();
        }
    }
}


코드가 이런데 자꾸 시간초과가 나네요 다른방법이 있나요?

댓글을 작성하려면 로그인해야 합니다.