Maison > développement back-end > C++ > le corps du texte

Maîtriser les modèles creux : un guide complet avec des exemples de code

WBOY
Libérer: 2024-07-16 19:00:58
original
577 Les gens l'ont consulté

Mastering Hollow Patterns: A Comprehensive Guide with Code Examples

Bienvenue dans notre guide complet sur la création de divers motifs creux à l'aide de boucles en programmation C ! Dans ce didacticiel, nous expliquerons étape par étape comment dessiner 18 motifs creux différents. Ces motifs vont des formes basiques comme les carrés et les triangles à des formes plus complexes comme les losanges, les hexagones et les pentagones. Chaque modèle est créé à l'aide de boucles imbriquées, ce qui en fait un excellent exercice pour les débutants souhaitant pratiquer les structures de contrôle en C. Allons-y !

Vous pouvez trouver tout le code dans notre référentiel GitHub.

Table des matières

  1. Introduction aux boucles imbriquées
  2. Carré Creux
  3. Triangle rectangle creux
  4. Triangle rectangle inversé creux
  5. Triangle creux aligné à droite
  6. Triangle inversé creux aligné à droite
  7. Triangle Pascal creux droit
  8. Triangle Pascal creux gauche
  9. Triangle équilatéral creux
  10. Triangle équilatéral inversé creux
  11. Pyramide creuse
  12. Pyramide inversée creuse
  13. Diamant creux
  14. Sablier creux
  15. Losange creux
  16. Parallélogramme creux
  17. Hexagone creux
  18. Pentagone creux
  19. Pentagone inversé creux
  20. Conclusion

Introduction aux boucles imbriquées

Avant de commencer avec les motifs, il est essentiel de comprendre le concept de boucles imbriquées. Une boucle imbriquée est une boucle à l’intérieur d’une autre boucle. Cette structure est particulièrement utile pour gérer des tableaux multidimensionnels et pour générer des modèles. En C, une structure de boucle imbriquée typique ressemble à ceci :

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // Code to execute
    }
}
Copier après la connexion

Carré creux

Explication :

  • Le motif carré creux se compose de n lignes et n colonnes.
  • Les caractères sont imprimés uniquement au niveau des bordures (première ligne, dernière ligne, première colonne et dernière colonne).
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");
}
Copier après la connexion

Sortie :

*  *  *  *  *  
*           *  
*           *  
*           *  
*  *  *  *  *  
Copier après la connexion

Triangle rectangle creux

Explication :

  • Le motif de triangle rectangle creux commence par un caractère dans la première rangée et augmente d'un caractère dans chaque rangée suivante.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonale).
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");
}
Copier après la connexion

Sortie :

*  
*  *  
*     *  
*        *  
*  *  *  *  *  
Copier après la connexion

Triangle rectangle inversé creux

Explication :

  • Le motif de triangle rectangle inversé creux commence par n caractères dans la première rangée et diminue d'un caractère dans chaque rangée suivante.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonale).
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");
}
Copier après la connexion

Sortie :

*  *  *  *  *  
*        *  
*     *  
*  *  
*  
Copier après la connexion

Triangle creux aligné à droite

Explication :

  • Le motif du triangle creux aligné à droite est similaire au triangle rectangle creux, mais le triangle est aligné à droite.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonale).
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");
}
Copier après la connexion

Sortie :

            *  
         *  *  
      *     *  
   *        *  
*  *  *  *  *  
Copier après la connexion

Triangle inversé creux aligné à droite

Explication :

  • Le motif de triangle inversé creux aligné à droite est à l'opposé du triangle creux aligné à droite.
  • Il commence par n caractères dans la première ligne et diminue d'un caractère dans chaque ligne suivante, mais le triangle est aligné à droite.
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");
}
Copier après la connexion

Sortie :

*  *  *  *  *  
   *        *  
      *     *  
         *  *  
            *  
Copier après la connexion

Triangle de Pascal droit creux

Explication :

  • Le motif de triangle Pascal rectangle creux combine le triangle rectangle et le triangle rectangle inversé pour former un triangle de type Pascal.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonales).
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");
}
Copier après la connexion

Sortie :

*  
*  *  
*     *  
*        *  
*           *  
*        *  
*     *  
*  *  
*  
Copier après la connexion

Triangle Pascal gauche creux

Explication :

  • Le motif du triangle de Pascal creux gauche est similaire au triangle de Pascal creux droit, mais il est aligné à gauche.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonales).
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");
}
Copier après la connexion

Sortie :

            *  
         *  *  
      *     *  
   *        *  
*           *  
   *        *  
      *     *  
         *  *  
            *  
Copier après la connexion

Triangle équilatéral creux

Explication :

  • Le motif de triangle équilatéral creux est symétrique et centré.
  • Les caractères sont imprimés uniquement au niveau des bordures (première rangée, dernière rangée et diagonales).
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");
}
Copier après la connexion

Sortie :

            *  
         *     *  
      *           *  
   *                 *  
*  *  *  *  *  *  *  *  * 
Copier après la connexion

Triangle équilatéral inversé creux

Explication :

  • 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");
}
Copier après la connexion

Output:

*  *  *  *  *  *  *  *  *  
   *                 *  
      *           *  
         *     *  
            *  
Copier après la connexion

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"); 
}
Copier après la connexion

Output:

    *
   * *
  *   *
 *     *
********* 
Copier après la connexion

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");
}
Copier après la connexion

Output:

*********
 *     *
  *   *
   * *
    * 
Copier après la connexion

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");
}
Copier après la connexion

Output:

    * 
   * * 
  *   * 
 *     * 
*       * 
 *     * 
  *   * 
   * * 
    * 
Copier après la connexion

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");
}
Copier après la connexion

Output:

* * * * * 
 *     * 
  *   * 
   * * 
    * 
   * * 
  *   * 
 *     * 
* * * * * 
Copier après la connexion

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");
}
Copier après la connexion

Output:

            *  *  *  *  *  
         *           *  
      *           *  
   *           *  
*  *  *  *  *  
Copier après la connexion

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");
}
Copier après la connexion

Output:

* * * * * * * * * * 
 *                 * 
  *                 * 
   *                 * 
    * * * * * * * * * *  
Copier après la connexion

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");
}
Copier après la connexion

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  * * * * * 
Copier après la connexion

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");
}
Copier après la connexion

Output:

      *
     * *
    *   *
   *     *
  *       *
 *         *
*           * 
 *         * 
  * * * * * 
Copier après la connexion

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");
}
Copier après la connexion

Output:

  * * * * * 
 *         * 
*           * 
 *         * 
  *       * 
   *     * 
    *   * 
     * * 
      *  
Copier après la connexion

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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:dev.to
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!