자바 패턴 문제

24가지 패턴 문제에 대한 Java 솔루션



패턴 문제 이면의 단순한 논리
  • 패턴의 행 수가 외부 루프에 의해 실행됩니다.
  • 내부 루프가 여러 열에 걸쳐 실행됨(열 번호 계산)
  • 각 열에 요소를 인쇄해야 함
  • 새로운 줄과 공백에 주의하세요

  • 패턴 1




    
         *
         * *
         * * *
         * * * *
         * * * * *
    
    
    
       public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            //outer loop iterates over n;
            for(int row = 1; row <=n; row++){
                //inner loop iterates over row;
                for(int col = 1; col <=row; col++){
                    //print *;
                    System.out.print("* ");
                }
                //print new line;
                System.out.println();
            }
        }
    


    패턴 2




    
         * * * * *
         * * * *
         * * *
         * *
         *
    
    
    
     public static void main(String[] args) {
            // number of lines in the pattern;
            int n = 5;
            // outer loop iterates over number of lines;
            for(int row = 1; row <= n; row++) {
                // inner loop iterates over the formula (n + 1)-row ;
                for(int col = 1; col <= n+1-row; col++) {
                    //print *
                    System.out.print("* ");
                }
                // add new line
                System.out.println();
            }
        }
    


    패턴 3




    
    
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    
    
     public static void main(String[] args) {
            // number of lines in the pattern
            int n = 5;
            //outer loop iterates over n;
            for(int row = 1; row <=n; row++){
                //inner loop iterates over row;
                for(int col = 1; col <=row; col++){
                    //print *;
                    System.out.print(col + " ");
                }
                //print new line;
                System.out.println();
            }
        }
    


    패턴 4




    
    
         *
         * *
         * * *
         * * * *
         * * * * *
         * * * *
         * * *
         * *
         *
    
    
    public static void main(String[] args) {
            // number of lines look similar
            int n = 5;
            // outer loop iterates over number of lines in the pattern
            for(int row = 1; row < 2*n; row++) {
                // calculate number of columns in a row
                int noOfCol = row > n ? 2*n - row : row;
                // inner loop iterates over number of columns
                for(int col = 1; col <= noOfCol; col++) {
                    // print *
                    System.out.print("* ");
                }
                // add new line after every row
                System.out.println();
            }
    
        }
    


    패턴 5




    
    
         * * * * *
         * * * *
         * * *
         * *
         *
         * *
         * * *
         * * * *
         * * * * *
    
    
      public static void main(String[] args) {
            // number of lines look similar
            int n = 5;
            // outer loop iterates over number of lines in the pattern
            for(int row = 1; row < 2*n; row++){
                // calculate number of columns in a row
                int noOfCols = row > n ? row+1-n : n+1-row;
                // inner loop iterates over number of columns
                for(int col = 1; col <= noOfCols; col++) {
                    // print *
                    System.out.print("* ");
                }
                // After printing each row, add a new line
                System.out.println();
            }
    
        }
    


    패턴 6




    
    
         *
         * *
         * * *
         * * * *
         * * * * *
         * * * * *
         * * * *
         * * *
         * *
         *
    
    
      public static void main(String[] args) {
            // number of lines look similar
            int n = 5;
            // outer loop iterates over number of lines in the pattern
            for(int row = 1; row <= 2*n; row++) {
                // calculate number of columns in a row
                int noOfCol = row > n ? 2*n - row+1 : row;
                // inner loop iterates over number of columns
                for(int col = 1; col <= noOfCol; col++) {
                    // print *
                    System.out.print("* ");
                }
                // add new line after every row
                System.out.println();
            }
        }
    


    패턴 7




    
    
         * * * * *
         * * * *
         * * *
         * *
         *
         *
         * *
         * * *
         * * * *
         * * * * *
    
    
        public static void main(String[] args) {
            // number of similar lines that don't repeated
            int n = 5;
            // outer loop iterates over number of lines
            for(int row = 1; row <=2*n; row++) {
                // calculate the number of columns in each row
                int noOfCols = row > n? row - n : (n+1)-row ;
                // inner loop iterates over number of columns
                for(int col = 1; col <= noOfCols; col++) {
                    // print *
                    System.out.print("* ");
                }
                // after printing each row, add a new line
                System.out.println();
            }
        }
    


    패턴 8




    
    
                 *
               * *
             * * *
           * * * *
         * * * * *
    
    
        public static void main(String[] args) {
            // number of lines in the pattern
            int n = 5;
            // outer loop iterates over Number of lines which is rows
            for(int row = 1; row <= n; row ++) {
                // number of columns for printing space
                int noOfSpaces = n-row;
                // inner loop iterates over Number of spaces
                for(int space = 1; space <= noOfSpaces; space ++) {
                    System.out.print("  ");
                }
                // number of columns for printing star
                int noOfStars = row;
                // inner loop iterates over Number of stars
                for(int star = 1; star <= noOfStars; star ++) {
                    System.out.print("* ");
                }
                // after printing each row, add a new line
                System.out.println();
            }
        }
    


    패턴 9




    
    
         * * * * *
           * * * *
             * * *
               * *
                 *
    
    
    
       public static void main(String[] args) {
    
    
            // number of lines in the pattern
            int n = 5;
    
            // outer loop iterates over Number of lines which is rows
            for(int row = 1; row <= n; row ++) {
                // number of columns for printing space
                int noOfSpaces = row-1;
    
                // inner loop iterates over Number of spaces
                for(int space = 1; space <= noOfSpaces; space ++) {
                    System.out.print("  ");
                }
    
                // number of columns for printing star
                int noOfStars = n+1-row;
    
                // inner loop iterates over Number of stars
                for(int star = 1; star <= noOfStars; star ++) {
                    System.out.print("* ");
                }
                // after printing each row, add a new line
                System.out.println();
            }
        }
    


    패턴 10




    
    
               *
             * * *
           * * * * *
         * * * * * * *
       * * * * * * * * *
    
    
    
        public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop executes until the number of lines is reached
            for(int row = 1; row <=n; row++) {
                // number of spaces in the columns
                int noOfSpaces = n - row;
                // inner loop executes until the number of spaces  is reached
                for(int space = 1; space <=noOfSpaces; space++) {
                    System.out.print("  ");
                }
                // number of stars in the columns
                int noOfStars = row + row-1;
                // inner loop executes until the number of stars is reached
                for(int star = 1; star <= noOfStars; star++) {
                    System.out.print("* ");
                }
                // after printing each row,add a new line
                System.out.println();
            }
    
        }
    


    패턴 11




    
    
         * * * * * * * * *
           * * * * * * *
             * * * * *
               * * *
                 *
    
    
        public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop executes until the number of lines is reached
            for(int row = 1; row <=n; row++) {
                // number of spaces in the columns
                int noOfSpaces = row-1;
                // inner loop executes until the number of spaces  is reached
                for(int space = 1; space <=noOfSpaces; space++) {
                    System.out.print("  ");
                }
                // number of stars in the columns
                int noOfStars = 2* (n-row)+1;
                // inner loop executes until the number of stars is reached
                for(int star = 1; star <= noOfStars; star++) {
                    System.out.print("* ");
                }
                // after printing each row,add a new line
                System.out.println();
            }
    
        }
    
    


    패턴 12




    
    
             *
            * *
           * * *
          * * * *
         * * * * *
    
    
        public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop iterates over Number of lines which is rows
            for(int row = 1; row <= n; row ++) {
                // number of columns for printing space
                int noOfSpaces = n-row;
                // inner loop iterates over Number of spaces
                for(int space = 1; space <= noOfSpaces; space ++) {
                    System.out.print(" ");
                }
                // number of columns for printing star
                int noOfStars = row;
                // inner loop iterates over Number of stars
                for(int star = 1; star <= noOfStars; star ++) {
                    System.out.print("* ");
                }
                // after printing each row, add a new line
                System.out.println();
            }
        }
    


    패턴 13




    
    
         * * * * *
          * * * *
           * * *
            * *
             *
    
    
       public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop iterates over Number of lines which is rows
            for(int row = 1; row <= n; row ++) {
                // number of columns for printing space
                int noOfSpaces = row-1;
                // inner loop iterates over Number of spaces
                for(int space = 1; space <= noOfSpaces; space ++) {
                    System.out.print(" ");
                }
                // number of columns for printing star
                int noOfStars = n-row+1;
                // inner loop iterates over Number of stars
                for(int star = 1; star <= noOfStars; star ++) {
                    System.out.print("* ");
                }
                // after printing each row, add a new line
                System.out.println();
            }
        }
    


    패턴 14




    
    
         * * * * *
          * * * *
           * * *
            * *
             *
             *
            * *
           * * *
          * * * *
         * * * * *
    
    
        public static void main(String[] args) {
            // number of lines in the pattern which is rows
            int N = 10;
            // number of similar lines in the pattern
            int n = 5;
            // outer loop iterates over the number of lines N
            for (int row = 1; row <= N; row++) {
                // number of spaces in the column
                int noOfSpaces = row > n ? N - row : row - 1;
                // inner loop iterates over the number of spaces
                for(int space = 1; space <= noOfSpaces; space++) {
                    System.out.print(" ");
                }
                // number of stars in the column
                int noOfStars = row > n ? row-n : n-row+1;
                // inner loop iterates over the number of stars
                for(int stars = 1; stars <= noOfStars; stars++) {
                    System.out.print("* ");
                }
                // add a new line after printing rows
                System.out.println();
            }
        }
    


    패턴 15




    
    
    
        *
       * *
      *   *
     *     *
    *********
    
    
       public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop will be executed till the number of lines
            for(int row = 1; row <=n; row++) {
                // number of spaces in the column before printing star
                int noOfSpaces = n -row;
                //inner loop executes until the noOfSpaces is reached
                for(int space = 1; space <=noOfSpaces; space++) {
                    // print spaces
                    System.out.print(" ");
                }
                // number of stars and spaces in the pattern
                int noOfStars = row + row -1;
                // inner loop executes until the noOfStars is reached
                for(int star = 1; star <= noOfStars; star++){
                    // print stars in first, last in the column and last row
                    if(star ==1 || star == noOfStars || row == n){
                        System.out.print("*");
                    }
                    // otherwise print spaces
                    else System.out.print(" ");
    
                }
                // adding a new line after printing each row
                System.out.println();
            }
    
        }
    


    패턴 16




    
    
         *********
          *     *
           *   *
            * *
             *
    
    
        public static void main(String[] args) {
    
            // number of lines in the pattern
            int n = 5;
            // outer loop will be executed till the number of lines
            for(int row = 1; row <=n; row++) {
                // number of spaces in the column before printing star
                int noOfSpaces = row-1;
                //inner loop executes until the noOfSpaces is reached
                for (int space = 1; space <= noOfSpaces; space++) {
                    // print spaces
                    System.out.print(" ");
                }
                // number of stars and spaces in the pattern
                int noOfStars = 2*n-row- noOfSpaces;
                // inner loop executes until the noOfStars is reached
                for (int star = 1; star <= noOfStars; star++) {
                    // print stars in first, last in the column and last row
                    if (star == 1 || star == noOfStars || row == 1) {
                        System.out.print("*");
                    }
                    // otherwise print spaces
                    else System.out.print(" ");
    
                }
                // adding a new line after printing each row
                System.out.println();
            }
        }
    


    패턴 17




    
    
         *****
         *   *
         *   *
         *   *
         *****
    
    
       public static void main(String[] args) {
    
            // number of lines in the pattern
            int l = 5;
            // outer loop executes over l
            for(int row = 1; row <=l; row ++) {
                // number of stars and spaces in a row;
                int noOfStars = l ;
                // inner loop executes over noOfStars;
                for(int star = 1; star <=noOfStars; star ++) {
                    // print star in column first,column last, row first and row last positions
                    if(star == 1 || star == noOfStars || row == l || row == 1){
                        System.out.print("*");
                    }
                    // print spaces
                    else System.out.print(" ");
                }
                // add a new line after each row
                System.out.println();
            }
    
        }
    
    


    패턴 18




    
    
         * * * * *
          * * * * *
           * * * * *
            * * * * *
             * * * * *
    
    
        public static void main(String[] args) {
    
            // total lines in the pattern
            int l = 5;
    
            // outer loop executes over the l
            for(int row = 1; row <= l; row++) {
    
                // number of Spaces before printing stars
                int noOfSpaces = row - 1;
                // inner loop executes over noOFSpaces;
                for(int space = 1; space <= noOfSpaces; space++) {
                    // print space
                    System.out.print(" ");
                }
                // number of stars;
                int noOfStars = l;
                // inner loop executes over noOfStars;
                for(int star = 1; star <= noOfStars; star++) {
                    // print star
                    System.out.print("* ");
                }
                // add a new line before executes next row
                System.out.println();
            }
        }
    
    


    패턴 19




    
    
             *****
            *   *
           *   *
          *   *
         *****
    
    
        public static void main(String[] args) {
    
            // number of lines in the pattern
            int l = 5;
            // outer loop for executing the number of rows which is equal to number of lines l
            for(int row = 1; row <=l; row ++) {
    
                // number of spaces in the column before printing stars
                int noOfSpaces = l - row;
                // inner loop executes over noOfSpaces
                for(int spaces = 1; spaces <= noOfSpaces; spaces++) {
                    // print stars before printing stars
                    System.out.print(" ");
                }
                // number of stars and middle spaces
                int noOfStars = l;
                // inner loop executes over noOfStars
                for(int stars = 1; stars <=noOfStars; stars++) {
                    // print stars in  first and last position of column and row
                    if(stars == 1 || stars == noOfStars || row == 1 || row == l){
                        System.out.print("*");
                    }
                    // otherwise print middle spaces
                    else System.out.print(" ");
                }
                // add a new line after each row
                System.out.println();
            }
        }
    
    


    패턴 20




    
    
         *****
          *   *
           *   *
            *   *
             *****
    
    
    
       public static void main(String[] args) {
    
            // number of lines in the pattern
            int l = 5;
            // outer loop for executing the number of rows which is equal to number of lines l
            for(int row = 1; row <=l; row ++) {
    
                // number of spaces in the column before printing stars
                int noOfSpaces = row -1;
                // inner loop executes over noOfSpaces
                for(int spaces = 1; spaces <= noOfSpaces; spaces++) {
                    // print stars before printing stars
                    System.out.print(" ");
                }
                // number of stars and middle spaces
                int noOfStars = l;
                // inner loop executes over noOfStars
                for(int stars = 1; stars <=noOfStars; stars++) {
                    // print stars in  first and last position of column and row
                    if(stars == 1 || stars == noOfStars || row == 1 || row == l){
                        System.out.print("*");
                    }
                    // otherwise print middle spaces
                    else System.out.print(" ");
                }
                // add a new line after each row
                System.out.println();
            }
        }
    


    패턴 21




    
    
    
         *
        * *
       *   *
      *     *
     *       *
      *     *
       *   *
        * *
         *
    
    
    
        public static void main(String[] args) {
            // number of lines
            int l = 9;
            // number of similar lines
            int n = l/2+1;
            // outer loop iterates over l;
            for(int row = 1; row <= l; row++) {
                // number of spaces before printing each column
                int noOfSpaces = row > n ? row - n : n - row ;
                // inner loop iterates over noOfSpaces;
                for(int space = 1; space <= noOfSpaces; space++) {
                    // print spaces
                    System.out.print(" ");
                }
                // number of stars in a column
                int noOfStars = row > n ?  2*n - row : row ;
                // inner loop iterates over noOfStars;
                for(int star = 1; star <= noOfStars; star++) {
                    // star print in first and last column in a row
                    if(star == 1 || star == noOfStars) {
                        System.out.print("* ");
                    }
                    // otherwise print spaces
                    else System.out.print("  ");
                }
                // add a new line after each row
                System.out.println();
            }
        }
    


    패턴 22




    
    
            1
           1 1
          1 2 1
         1 3 3 1
        1 4 6 4 1
    
    
    
        public static void main(String[] args) {
    
            // number of lines
            int n = 5;
            // outer loop iterates over n;
            for(int row = 1; row <= n; row++) {
                // set num = 1;
                int num = 1;
                // number of spaces before printing elements
                int noOfSpaces = n - row ;
                // outer loop iterates over noOfSpaces;
                for(int space = 1; space <= noOfSpaces; space++) {
                    // print spaces
                    System.out.print(" ");
                }
                // inner loop iterates over row;
                for(int col = 1 ; col <= row; col++) {
                    // print num;
                    System.out.print(num + " ");
                    // update num value for next number
                    num = num * (row - col)/ (col);
                }
                // add a new line
                System.out.println();
            }
        }
    


    패턴 23




    
    
    
       1
      212
     32123
    4321234
     32123
      212
       1
    
    
    
        public static void main(String[] args) {
    
            // number of input
            int n = 4;
            // number of lines
            int N = 2*n-1;
            // outer loop iterates over N;
            for(int row = 1; row <= N; row++) {
                // number of spaces in a row
                int noOfSpaces = row > n ? row - n : n - row ;
                // inner loop iterates over noOfSpaces;
                for(int space = 1; space <= noOfSpaces; space++) {
                    // print space
                    System.out.print(" ");
                }
                // number of cols c;
                int c = row > n ? 2*n-row : row ;
                // from c to 1
                for(int col = c; col >= 1 ; col--) {
                    System.out.print(col);
                }
                // from 2 to c;
                for(int col = 2; col <=c ; col++) {
                    System.out.print(col);
                }
                System.out.println();
            }
        }
    
    


    패턴 24




    
    
    
    1
    0 1
    1 0 1
    0 1 0 1
    1 0 1 0 1
    
    
    
        public static void main(String[] args) {
            // number of lines
            int n = 5;
            // outer loop iterates over n;
            for (int row = 1; row <= n; row++) {
    
                // inner loop executes each col till number of rows;
                for (int col = 1; col <= row; col++) {
                    // print 1 for even
                    if ((row + col) % 2 == 0) {
                        System.out.print(1+ " ");
                    }
                    // print 0 for odd
                    else {
                        System.out.print(0+ " ");
                    }
                }
                // add a new line
                System.out.println();
            }
        }
    

    좋은 웹페이지 즐겨찾기