You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何用C语言打印10行递增数字+递减字母的三角形图案?请说明逻辑

How to Print the Specified 10-Line Triangle Pattern in C

Alright, let's figure out how to print this specific 10-line triangle pattern in C—we'll break it down step by step so you understand exactly what's going on.

First, here's the pattern we're targeting:

1
12A
123BA
1234CBA
12345DCBA
123456EDCBA
1234567FEDCBA
12345678GFEDCBA
123456789HGFEDCBA
12345678910IHGFEDCBA

Pattern Breakdown

Let's dissect each line to spot the repeating rules:

  • For the n-th line (starting at 1 and going up to 10):
    1. The first part counts sequentially from 1 to n. A nice thing here is that when n hits 10, we just print the full number—no special handling needed for two-digit values.
    2. The second part is a reverse alphabet sequence, starting from the (n-1)-th uppercase letter and going back to 'A'. For example:
      • Line 2 (n=2): Only 'A' (since 2-1=1, which is the first letter in the alphabet)
      • Line 3 (n=3): 'B' followed by 'A' (3-1=2, the second letter is B)
      • Line 10 (n=10): 'I' all the way down to 'A' (10-1=9, the ninth letter is I)

Complete C Code

#include <stdio.h>

int main() {
    const int total_lines = 10; // We're making this a constant for clarity
    
    // Loop through each line from 1 to 10
    for (int line = 1; line <= total_lines; line++) {
        // Print the increasing number sequence (1 to current line number)
        for (int num = 1; num <= line; num++) {
            printf("%d", num);
        }
        
        // Calculate the starting letter for the reverse alphabet segment
        // For line 1, this will be a character before 'A', so the loop won't run
        char start_letter = 'A' + (line - 2);
        
        // Print letters from start_letter down to 'A'
        for (char c = start_letter; c >= 'A'; c--) {
            printf("%c", c);
        }
        
        // Move to the next line after finishing the current one
        printf("\n");
    }
    
    return 0;
}

How This Code Works

Let's walk through each part to demystify it:

  1. Outer Loop: The for loop runs from 1 to 10, handling each line one by one. Using a const for total_lines makes it easy to adjust the number of lines later if you want.
  2. Number Sequence: The inner loop prints numbers from 1 up to the current line number. Since we're using printf("%d"), it automatically handles single-digit (1-9) and two-digit (10) numbers without extra code—handy!
  3. Reverse Alphabet Sequence:
    • For line 1: line - 2 is -1, so start_letter becomes 'A' - 1 (a character before 'A'). The loop condition c >= 'A' fails right away, so no letters are printed (perfect, since the first line only has "1").
    • For line 2: start_letter is 'A' + 0 = 'A', so we print just 'A'.
    • For line 10: start_letter is 'A' + 8 = 'I', so we loop from 'I' back to 'A' printing each character.
  4. New Line: After each line's numbers and letters are printed, printf("\n") moves us to the next line to start fresh.

Quick Test

Compile and run this code, and you'll get exactly the pattern you asked for. If you ever want more or fewer lines, just change the value of total_lines—the logic scales perfectly.

内容的提问来源于stack exchange,提问作者Aditya Pandey

火山引擎 最新活动