>  기사  >  백엔드 개발  >  속이 빈 피라미드와 마름모 패턴을 인쇄하는 C 프로그램

속이 빈 피라미드와 마름모 패턴을 인쇄하는 C 프로그램

PHPz
PHPz앞으로
2023-09-02 11:25:061305검색

속이 빈 피라미드와 마름모 패턴을 인쇄하는 C 프로그램

여기에서는 C 언어를 사용하여 속이 빈 피라미드와 다이아몬드 패턴을 생성하는 방법을 살펴보겠습니다. 견고한 피라미드 패턴을 쉽게 생성할 수 있습니다. 빈 공간으로 만들려면 몇 가지 트릭을 추가해야 합니다.

Hollow Pyramid

첫 번째 행의 피라미드의 경우 마지막 행에 별표 1개와 별표 n개를 인쇄합니다. 다른 줄의 경우 줄의 시작과 끝 부분에 두 개의 별표를 인쇄하며 두 별표 사이에 약간의 공백이 있습니다.

예제 코드

#include <stdio.h>
int main() {
   int n, i, j;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   for(i = 1; i<=n; i++) {
      for(j = 1; j<=(n-i); j++){ //print the blank spaces before star
         printf(" ");
      }
      if(i == 1 || i == n){ //for the first and last line, print the
         stars continuously
         for(j = 1; j<=i; j++) {
            printf("* ");
         }
      } else {
         printf("*"); //in each line star at start and end
         position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            printf(" ");
         }
         printf("*");
      }
      printf("</p><p>");
   }
}

Output

Enter number of lines: 20
                                         *
                                       *   *
                                      *     *
                                     *       *
                                    *         *
                                   *           *
                                  *             *
                                 *               *
                                *                 *
                               *                   *
                              *                     *
                             *                       *
                            *                         *
                           *                           *
                          *                             *
                         *                               *
                        *                                 *
                       *                                   *
                      *                                     *
                     * * * * * * * * * * * * * * * *  * * *  * 

빈 마름모

마름모의 첫 번째와 마지막 행에 대해 별표를 인쇄합니다. 다른 줄의 경우 줄의 시작과 끝 부분에 두 개의 별이 인쇄되며 두 시작 사이에 약간의 공백이 있습니다. 다이아몬드는 두 부분으로 이루어져 있습니다. 상부 및 하부 절반. 위쪽 부분에서는 공백 수를 늘려야 하고, 아래쪽 부분에서는 공백 수를 줄여야 합니다. 여기서는 mid라는 또 다른 변수를 사용하여 줄 번호를 두 부분으로 나눌 수 있습니다.

예제 코드

#include <stdio.h>
int main() {
   int n, i, j, mid;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   if(n %2 == 1) { //when n is odd, increase it by 1 to make it even
      n++;
}
mid = (n/2);
for(i = 1; i<= mid; i++) {
    for(j = 1; j<=(mid-i); j++){ //print the blank spaces before star
      printf(" ");
    }
    if(i == 1) {
      printf("*");
    } else {
      printf("*"); //in each line star at start and end
      position
      for(j = 1; j<=2*i-3; j++){ //print space to make hollow
          printf(" ");
      }
      printf("*");
    }
    printf("</p><p>");
}
for(i = mid+1; i<n; i++) {
   for(j = 1; j<=i-mid; j++) { //print the blank spaces before star
      printf(" ");
   }
   if(i == n-1) {
      printf("*");
   } else {
      printf("*"); //in each line star at start and end
      position
      for(j = 1; j<=2*(n - i)-3; j++) { //print space to make
        hollow
        printf(" ");
      }
      printf("*");
   }
   printf("</p><p>");
}

Output

Enter number of lines: 15
                      *
                    *   *
                  *       *
                 *         *
                *           *
               *             *
              *               *
             *                 *
              *               *
               *             *
                *           *
                 *         *
                   *     *
                    *   *
                      *

위 내용은 속이 빈 피라미드와 마름모 패턴을 인쇄하는 C 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제