中空パターンのマスタリング: コード例を含む包括的なガイド

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");
}
ログイン後にコピー

出力:

*  *  *  *  *  
*           *  
*           *  
*           *  
*  *  *  *  *  
ログイン後にコピー

中空直角三角形

説明:

  • 中空の直角三角形パターンは、最初の行の 1 文字から始まり、後続の行ごとに 1 文字ずつ増加します。
  • 文字は境界線(先頭行、最終行、対角線)のみに印刷されます。
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 文字で始まり、後続の行ごとに 1 文字ずつ減少します。
  • 文字は境界線(先頭行、最終行、対角線)のみに印刷されます。
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 文字で始まり、後続の行ごとに 1 文字ずつ減少しますが、三角形は右揃えになります。
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 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!