首页 > 后端开发 > 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学习者快速成长!