如何在C语言中实现棋盘图案?当前printf方式无效,请求技术帮助
Hey Kenny, let's iron out that checkerboard pattern you're struggling with in C! Using fixed printf calls like you're doing right now can't adapt to different input values, so we'll switch to nested loops to dynamically generate the pattern based on your input number.
First, let's align on what we're building from your examples:
- Input 5 gives a line like
***** ***** ***** ***** ***** *****(6 groups of 5 asterisks separated by spaces) - Input 2 gives a longer string with 12 of those same
*****groups - You mentioned the logic mirrors integer matrix summation—so we'll use index arithmetic (like row + column sums) to drive the pattern, just like how you'd calculate values in a matrix.
Here's a solution that builds on your existing code skeleton:
Dynamic Checkerboard Implementation (Matching Matrix Summation Logic)
#include <stdio.h> int main() { int n; // Your input value (5, 2, 3, etc.) int row, block, char_idx; const int blocks_per_row = 6; // Matches your input 5 example's 6 groups const int total_rows = 6; // Adjust this for how many rows you want in the full board printf("Enter your number: "); scanf("%d", &n); // Outer loop: iterate over each row of the board for (row = 0; row < total_rows; row++) { // Middle loop: iterate over each block in the current row for (block = 0; block < blocks_per_row; block++) { // Use matrix-like summation (row + block index) to decide content if ((row + block) % 2 == 0) { // Print n asterisks for this block for (char_idx = 0; char_idx < n; char_idx++) { printf("*"); } } else { // Print n spaces for this block (creates the checkerboard gap) for (char_idx = 0; char_idx < n; char_idx++) { printf(" "); } } // Add a space between blocks to match your example formatting printf(" "); } // Move to the next row printf("\n"); } return 0; }
What This Code Does:
- Takes your input
nwhich sets the width of each "cell" in the checkerboard (so input 5 makes each asterisk/space block 5 characters wide) - Uses the sum of the row index and block index (just like matrix element coordinates) to alternate between asterisk blocks and space blocks—this is the matrix summation-style logic you referenced
- Creates a consistent grid with
blocks_per_rowblocks per line andtotal_rowstotal rows (tweak these constants to match your exact needs)
If You Want a Repeated Asterisk Pattern (No Alternating Spaces)
If your example was meant to show a single line (or multiple lines) of repeated ***** groups instead of a classic checkerboard, here's a simplified version that adjusts the repeat count based on input:
#include <stdio.h> int main() { int n; int repeat_idx, row_idx; int repeat_count; printf("Enter your number: "); scanf("%d", &n); // Map input n to the number of ***** groups (adjust this formula to match your exact rules) if (n == 5) repeat_count = 6; else if (n == 2) repeat_count = 12; else if (n == 3) repeat_count = 10; // Update this to your expected count for input 3 else repeat_count = 6; // Default fallback // Print a single line of repeated groups for (repeat_idx = 0; repeat_idx < repeat_count; repeat_idx++) { printf("***** "); } printf("\n"); // Uncomment below if you want multiple rows (e.g., n rows total) // for (row_idx = 0; row_idx < n; row_idx++) { // for (repeat_idx = 0; repeat_idx < repeat_count; repeat_idx++) { // printf("***** "); // } // printf("\n"); // } return 0; }
If you can share the exact rule for how input n translates to the pattern (like "input n gives n rows, each with (12 - n) asterisk blocks"), we can refine this even further to match your exact expectations.
内容的提问来源于stack exchange,提问作者Kenny Doug

