首页 > 后端开发 > C++ > 正文

在C语言中打印对称的双三角形图案

王林
发布: 2023-09-14 12:53:02
转载
1391 人浏览过

在C语言中打印对称的双三角形图案

给定行数,程序必须以最小的复杂性打印对称双三角形图案。

示例

Input: 5
Output:  X
          X
          O X
           O X
      X O X O X
       X O
        X O
         X
          X
登录后复制

整个问题包含3个不同的分区 −

  • 对于奇数n,打印上半部分的n-1行,对于偶数n,打印上半部分的n-2行。

  • 打印中间行,对于奇数n,打印1行,对于偶数n,打印3行。

  • 打印下半部分,对于奇数n,打印n-1行,对于偶数n,打印n-2行。

算法

START
   STEP 1: IF (n % 2 == 0) then
      x = x - 1;
      Define p as n – 1 for spaces
      Define s = 1 for characters
   STEP 2: LOOP FOR i= 1 AND i <= (x - 1) / 2 AND i++
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END LOOP
      IF i % 2 != 0 then
         GOTO STEP 11 Printx(s)
      ELSE
         GOTO STEP 12 Printo(s)
      END
      PRINT NEW LINE
      INCREMENT p BY 1
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END LOOP
      IF i % 2 != 0 then,
         GOTO STEP 11 Printx(s)
      ELSE
         GOTO STEP 12 Printo(s)
      END IF
         PRINT NEWLINE
         DECREMENT p BY 1
         INCREMENT s BY 1
      END LOOP
   STEP 3: IF n % 2 == 0 {
      LOOP FOR i = 1 AND i <= p AND i++
         PRINT SPACE
         IF n % 4 != 0 then,
            GOTO STEP 11 Printx(n/2)
         ELSE
            GOTO STEP 12 Printo(n/2)
         END IF
         PRINT SPACE
      END IF
   STEP 4: IF n % 2 != 0 then,
      GOTO STEP
      ELSE
         IF n % 4 != 0 then,
            DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printx(n/2)
         ELSE
            DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2) and Printo(n/2)
            DIVIDE n BY 2 AND GOTO STEP
         END IF
      END IF
      PRINT NEW LINE
   STEP 5: IF n % 2 == 0 then, {
      PRINT SPACE
      DIVIDE n BY 2 AND GOTO STEP 11 Printx(n/2)
      PRINT NEWLINE
      END IF
   STEP 6: SET p = 1
   STEP 7: IF n % 2 == 0
      DECREMENT x BY 1
      SET p = 2
      END IF
   STEP 8: SET q = x / 2
   STEP 9: LOOP FOR i = 1 AND i <= x AND i++) {
      LOOP FOR j = 1 AND j <= p AND j++
         PRINT SPACE
      END FOR
      PASS q TO STEP 11 Printx(q)
      IF i % 2 == 0 THEN
         DECREMENT q BY 1
      END IF
      PRINT NEWLINE
      INCREMENT p BY 1
   END FOR
   STEP 10: PRINT NEWLINE
   STEP 11: Printx(n)
      LOOP FOR i = 1 AND i< = n AND i++
         IF i % 2 != 0 then,
            PRINT x
         ELSE
            PRINT o
      END IF
   END FOR
   STEP 12: Printo(n)
      LOOP FOR i = 1 AND i< = n AND i++
      IF i % 2 != 0 then,
         PRINT o
      ELSE
      PRINT x
STOP
登录后复制

示例

#include <stdio.h>
// printing alternate x o starting with x
int printx(int n) {
   int i;
   for ( i = 1; i < = n; i++) {
      if (i % 2! = 0)
         printf("x ");
      else
         printf("o ");
   }
   return 0;
}
// printing alternate x o starting with o
int printo(int n) {
   int i;
   for ( i = 1; i < = n; i++) {
      if (i % 2! = 0)
         printf("o ");
      else
         printf("x ");
   }
   return 0;
}
// To print the pattern for n
int printpattern(int n) {
   // upper half
   // n-1 lines for odd, n-2 lines for even
   int x = n;
   int i, j;
   if (n % 2 == 0)
   x = x - 1;
   // number of spaces to leave in each line
   int p = n - 1;
   // number of characters in each line
   int s = 1;
   // prints double lines in each iteration
   for ( i = 1; i < = (x - 1) / 2; i++) {
      for ( j = 1; j < = p; j++) {
         printf(" ");
      }
      if (i % 2! = 0)
         printx(s);
      else
         printo(s);
         printf("</p><p>");
         p++;
      for ( j = 1; j < = p; j++)
         printf(" ");
      if (i % 2! = 0)
         printx(s);
      else
         printo(s);
         printf("</p><p>");
         p--;
         s++;
   }
   // extra upper middle for even
   if (n % 2 == 0) {
      for ( i = 1; i < = p; i++)
         printf(" ");
      if (n % 4! = 0)
         printx(n / 2);
      else
         printo(n / 2);
         printf("</p><p>");
   }
   // middle line
   if (n % 2! = 0)
      printx(n);
   else {
      if (n % 4! = 0) {
         printx(n / 2);
         printx(n / 2);
      } else {
         printx(n / 2);
         printo(n / 2);
      }
   }
   printf("</p><p>");
   // extra lower middle for even
   if (n % 2 == 0) {
      printf(" ");
      printx(n / 2);
      printf("</p><p>");
   }
   // lower half
   p = 1;
   if (n % 2 == 0) {
      x--;
      p = 2;
   }
   int q = x / 2;
   // one line for each iteration
   for ( i = 1; i < = x; i++) {
      for (int j = 1; j < = p; j++)
         printf(" ");
         printx(q);
      if (i % 2 == 0)
         q--;
         printf("</p><p>");
         p++;
   }
   printf("</p><p>");
   return 1;
}
int main() {
   int n = 5;
   printpattern(n);
   return 0;
}
登录后复制

输出

如果我们运行上面的程序,它将生成以下输出。

      X
       X
       O X
        O X
   X O X O X
    X O
     X O
      X
       X
登录后复制

以上是在C语言中打印对称的双三角形图案的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板