> 백엔드 개발 > C++ > 본문

빈 패턴 마스터하기: 코드 예제가 포함된 종합 가이드

WBOY
풀어 주다: 2024-07-16 19:00:58
원래의
577명이 탐색했습니다.

Mastering Hollow Patterns: A Comprehensive Guide with Code Examples

C 프로그래밍에서 루프를 사용하여 다양한 중공 패턴 생성에 대한 종합 가이드에 오신 것을 환영합니다! 이 튜토리얼에서는 18개의 서로 다른 속이 빈 패턴을 그리는 방법에 대한 단계별 지침을 안내합니다. 이러한 패턴은 정사각형, 삼각형과 같은 기본 모양부터 다이아몬드, 육각형, 오각형과 같은 보다 복잡한 형태까지 다양합니다. 각 패턴은 중첩 루프를 사용하여 생성되므로 초보자가 C의 제어 구조를 연습할 수 있는 훌륭한 연습이 됩니다. 살펴보겠습니다!

GitHub 저장소에서 모든 코드를 찾을 수 있습니다.

목차

  1. 중첩 루프 소개
  2. 빈 사각형
  3. 빈 직각삼각형
  4. 빈 역직각삼각형
  5. 빈 오른쪽 정렬 삼각형
  6. 빈 오른쪽 정렬 역삼각형
  7. 빈 오른쪽 파스칼 삼각형
  8. 빈 왼쪽 파스칼 삼각형
  9. 빈 정삼각형
  10. 빈 역정삼각형
  11. 빈 피라미드
  12. 빈 역피라미드
  13. 할로우 다이아몬드
  14. 빈 모래시계
  15. 빈 마름모
  16. 중공 평행사변형
  17. 중공 육각형
  18. 할로우 펜타곤
  19. 빈 역오각형
  20. 결론

중첩 루프 소개

패턴을 시작하기 전에 중첩 루프의 개념을 이해하는 것이 중요합니다. 중첩 루프는 다른 루프 내부의 루프입니다. 이 구조는 다차원 배열을 처리하고 패턴을 생성하는 데 특히 유용합니다. C에서 일반적인 중첩 루프 구조는 다음과 같습니다.

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // Code to execute
    }
}
로그인 후 복사

중공 광장

설명:

  • 빈 사각형 패턴은 n개의 행과 n개의 열로 구성됩니다.
  • 문자는 테두리(첫 번째 행, 마지막 행, 첫 번째 열, 마지막 열)에만 인쇄됩니다.
int n = 5; // size of the square
char ch = '*';

printf("1. Hollow Square:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

*  *  *  *  *  
*           *  
*           *  
*           *  
*  *  *  *  *  
로그인 후 복사

속이 빈 직각삼각형

설명:

  • 빈 직각 삼각형 패턴은 첫 번째 행에서 한 문자로 시작하고 각 후속 행에서 한 문자씩 증가합니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("2. Hollow Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

*  
*  *  
*     *  
*        *  
*  *  *  *  *  
로그인 후 복사

속이 빈 역직각삼각형

설명:

  • 빈 역직각 삼각형 패턴은 첫 번째 행에서 n자로 시작하고 다음 행에서 한 문자씩 감소합니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("3. Hollow Inverted Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

*  *  *  *  *  
*        *  
*     *  
*  *  
*  
로그인 후 복사

빈 오른쪽 정렬 삼각형

설명:

  • 빈 오른쪽 정렬 삼각형 패턴은 속이 빈 직각 삼각형과 유사하지만 삼각형이 오른쪽 정렬입니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("4. Hollow Right Aligned Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

            *  
         *  *  
      *     *  
   *        *  
*  *  *  *  *  
로그인 후 복사

빈 오른쪽 정렬된 역삼각형

설명:

  • 빈 오른쪽 정렬 역삼각형 패턴은 빈 오른쪽 정렬 삼각형의 반대입니다.
  • 첫 번째 행에서는 n자로 시작하고 다음 행마다 한 문자씩 감소하지만 삼각형은 오른쪽 정렬됩니다.
printf("5. Hollow Right Aligned Inverted Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 1; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

*  *  *  *  *  
   *        *  
      *     *  
         *  *  
            *  
로그인 후 복사

빈 오른쪽 파스칼 삼각형

설명:

  • 빈 직각삼각형 패턴은 직각삼각형과 역직각삼각형을 결합하여 파스칼 모양의 삼각형을 형성하는 패턴입니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("6. Hollow Right Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
for (int i = 0; i < n; i++) {
    for (int j = n; j > i + 1; j--) {
        if (j == n || j == i + 2) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

*  
*  *  
*     *  
*        *  
*           *  
*        *  
*     *  
*  *  
*  
로그인 후 복사

빈 왼쪽 파스칼 삼각형

설명:

  • 빈 왼쪽 파스칼 삼각형 패턴은 속이 빈 오른쪽 파스칼 삼각형 패턴과 유사하지만 왼쪽 정렬됩니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("7. Hollow Left Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n - 1; j > i; j--) {
        if (j == n

 - 1 || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

            *  
         *  *  
      *     *  
   *        *  
*           *  
   *        *  
      *     *  
         *  *  
            *  
로그인 후 복사

속이 빈 정삼각형

설명:

  • 속이 빈 정삼각형 패턴은 대칭이며 중앙에 위치합니다.
  • 문자는 테두리(첫 번째 줄, 마지막 줄, 대각선)에만 인쇄됩니다.
printf("8. Hollow Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < 2 * i + 1; j++) {
        if (j == 0 || j == 2 * i || i == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

출력:

            *  
         *     *  
      *           *  
   *                 *  
*  *  *  *  *  *  *  *  * 
로그인 후 복사

속이 빈 역정삼각형

설명:

  • The hollow inverted equilateral triangle pattern is the opposite of the hollow equilateral triangle.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("9. Hollow Inverted Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        printf("   ");
    }
    for (int j = 2 * n - 1; j > 2 * i; j--) {
        if (j == 2 * n - 1 || j == 2 * i + 1 || i == 0) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

*  *  *  *  *  *  *  *  *  
   *                 *  
      *           *  
         *     *  
            *  
로그인 후 복사

Hollow Pyramid

Explanation:

  • The hollow pyramid pattern is centered and symmetrical.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("10. Hollow Pyramid:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (2 * i + 1); j++) {
        if (i == n - 1 || j == 0 || j == i * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
    printf("\n"); 
}
로그인 후 복사

Output:

    *
   * *
  *   *
 *     *
********* 
로그인 후 복사

Hollow Inverted Pyramid

Explanation:

  • The hollow inverted pyramid pattern is the opposite of the hollow pyramid.
  • Characters are printed only at the borders (first row, last row, and the diagonals).
printf("11. Hollow Inverted Pyramid:\n");
for (i = n; i > 0; i--) {
    for (j = n - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < (2 * i - 1); j++) {
        if (j == 0 || i == n  || j == (i-1) * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
  printf("\n");
}
로그인 후 복사

Output:

*********
 *     *
  *   *
   * *
    * 
로그인 후 복사

Hollow Diamond

Explanation:

  • The hollow diamond pattern is symmetrical and centered.
  • It consists of a hollow upper and lower triangle.
printf("12. Hollow Diamond:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 0; i < n; i++) {
    for (j = 0; j < i + 1; j++) {
        printf(" ");
    }
    for (j = n - 1; j > i; j--) {
        if (j == n - 1 || j == i + 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

    * 
   * * 
  *   * 
 *     * 
*       * 
 *     * 
  *   * 
   * * 
    * 
로그인 후 복사

Hollow Hourglass

Explanation:

  • The hollow hourglass pattern is symmetrical and centered.
  • It consists of a hollow upper and lower inverted triangle.
printf("13. Hollow Hourglass:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < (n - i) ; j++) {
        if (j == 0 || i == 0  || j ==  n - i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 1; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
       if (i == n - 1 || j == 0 || j == i) {
           printf("%c ", ch);
       } else {
           printf("  ");
       }
     }
     printf("\n");
}
로그인 후 복사

Output:

* * * * * 
 *     * 
  *   * 
   * * 
    * 
   * * 
  *   * 
 *     * 
* * * * * 
로그인 후 복사

Hollow Rhombus

Explanation:

  • The hollow rhombus pattern is symmetrical and centered.
  • Characters are printed only at the borders.
printf("14. Hollow Rhombus:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

            *  *  *  *  *  
         *           *  
      *           *  
   *           *  
*  *  *  *  *  
로그인 후 복사

Hollow Parallelogram

Explanation:

  • The hollow parallelogram pattern is symmetrical and slanted to one side.
  • Characters are printed only at the borders.
printf("15. Hollow Parallelogram:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < n * 2; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n * 2 - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

* * * * * * * * * * 
 *                 * 
  *                 * 
   *                 * 
    * * * * * * * * * *  
로그인 후 복사

Hollow Hexagon

Explanation:

  • The hollow hexagon pattern consists of a combination of upper and lower triangles and a middle section.
  • Characters are printed only at the borders.
printf("16. Hollow Hexagon:\n");
for (i = 0; i < n / 2; i++) {
    for (j = n / 2 - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < n + 1 * i; j++) {
        if ( i == 0 || j == 0 || j == n * i) {
           printf("%c ", ch);
        } else {
           printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  * * * * * 
로그인 후 복사

Hollow Pentagon

Explanation:

  • The hollow pentagon pattern consists of an upper triangle and a lower rectangle.
  • Characters are printed only at the borders.
printf("17. Hollow Pentagon:\n");
for (i = 0; i < n+1; i++) {
    for (j = n ; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
        if ( j == 0 || i == 0 || j == i ) {
            printf(" %c", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n +  i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

      *
     * *
    *   *
   *     *
  *       *
 *         *
*           * 
 *         * 
  * * * * * 
로그인 후 복사

Hollow Inverted Pentagon

Explanation:

  • The hollow inverted pentagon pattern consists of an upper inverted triangle and a lower inverted rectangle.
  • Characters are printed only at the borders.
printf("18. Hollow Inverted Pentagon:\n");
for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (int i = n + 1; i > 0; i--) {
    for (int j = n + 2; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j < i; j++) {
        if ( j == 0 || j == i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
로그인 후 복사

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  *       * 
   *     * 
    *   * 
     * * 
      *  
로그인 후 복사

Conclusion

In conclusion, we have explored a variety of patterns using loops and conditional statements in C, each producing different geometric shapes and designs. These patterns include solid and hollow variants of squares, triangles, pyramids, diamonds, hourglasses, rhombuses, parallelograms, hexagons, and pentagons. Understanding and implementing these patterns helps to strengthen programming logic, loop constructs, and conditionals, which are fundamental concepts in computer science.

By practicing these patterns, you can enhance your problem-solving skills and improve your ability to visualize and implement complex patterns in code. These exercises also provide a solid foundation for more advanced programming tasks and algorithms.

위 내용은 빈 패턴 마스터하기: 코드 예제가 포함된 종합 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!