C语言编程,C++环境下可以编译运行

输入行数和起始字母,输出大写字母等腰三角形.输出的字母是循环的,即递增到字母 'Z' 以后输出 'A' ;递减到字母 'A' 以后输出 'Z'.
例如
输入: 4 X↙
输出: X↙
YZY↙
ZABAZ↙
ABCDCBA↙

#include <stdio.h>

int main()
{
    int n, i, j;
    char st;
    scanf ("%d %c", &n, &st);
    char row = st, col;
    for (i = 0; i < n; ++i){
        for (j = 0; j < n - i - 1; ++j){
            printf (" ");
        }
        col = row;
        for (j = 0; j <= i; ++j){
            printf ("%c", col);
            if (col=='Z'){
                col = 'A';
            }else {
                col++;
            }
        }
        if (col=='A'){
            col = 'Z';
        }else {
            col--;
        }
        for (j = 0; j < i; ++j){
            if (col=='A'){
                col = 'Z';
            }else {
                col--;
            }
            printf ("%c", col);
        }
        printf ("\n");
        if (row == 'Z'){
            row = 'A';
        }else {
            row++;
        }
    }
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-12
/*
输入行数和起始字母,输出大写字母等腰三角形.输出的字母是循环的,即递增到字母 'Z' 以后输出 'A' ;递减到字母 'A' 以后输出 'Z'.
例如
输入: 4 X↙
输出: X↙
      YZY↙
     ZABAZ↙
    ABCDCBA↙
*/
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
    int ll, lines;
    char start_with;
    switch (argc)
    {
    case 0: 
        printf("Something is wrong, but I don't know why?\n"); 
        break;
    case 1: 
        printf("Input line numbers and char to start with:"); scanf("%d%c", &lines, &start_with);
        break;
    case 2: 
        lines=atoi(argv[1]); 
        if(lines<1) start_with = argv[1][0];
        else {
            printf("Input a char to start with:"); scanf("%c", &start_with);
        }
        break;
    case 3:
        lines=atoi(argv[1]); 
        start_with = argv[2][0];
    }
    if(!(start_with >= 'A' && start_with <= 'Z' || start_with >= 'a' && start_with <= 'z')) {
        printf("Wrong input.\n");
        return -1;
    }
    else if(start_with>='a') {
        start_with=start_with-'a'+'A';
    }
    printf("Print [%d] lines start with [%c].\n", lines, start_with);
    for(ll=0; ll<lines; ll++) {
        int pos;
        printf("%-*d|", (int)(log10(lines+1))+1, ll+1);
        printf("%*s", lines-ll-1, "");
        for(pos=-(ll+1); pos<(ll); pos++) {
            char tmp=start_with-abs(pos+1);
            while(tmp<'A') tmp+=26;
            printf("%c", tmp);
        }
        printf("\n");
        start_with+=2;
        if(start_with>'Z') start_with-=26;
    }
}

 

1 |                        A
2 |                       BCB
3 |                      CDEDC
4 |                     DEFGFED
5 |                    EFGHIHGFE
6 |                   FGHIJKJIHGF
7 |                  GHIJKLMLKJIHG
8 |                 HIJKLMNONMLKJIH
9 |                IJKLMNOPQPONMLKJI
10|               JKLMNOPQRSRQPONMLKJ
11|              KLMNOPQRSTUTSRQPONMLK
12|             LMNOPQRSTUVWVUTSRQPONML
13|            MNOPQRSTUVWXYXWVUTSRQPONM
14|           NOPQRSTUVWXYZAZYXWVUTSRQPON
15|          OPQRSTUVWXYZABCBAZYXWVUTSRQPO
16|         PQRSTUVWXYZABCDEDCBAZYXWVUTSRQP
17|        QRSTUVWXYZABCDEFGFEDCBAZYXWVUTSRQ
18|       RSTUVWXYZABCDEFGHIHGFEDCBAZYXWVUTSR
19|      STUVWXYZABCDEFGHIJKJIHGFEDCBAZYXWVUTS
20|     TUVWXYZABCDEFGHIJKLMLKJIHGFEDCBAZYXWVUT
21|    UVWXYZABCDEFGHIJKLMNONMLKJIHGFEDCBAZYXWVU
22|   VWXYZABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBAZYXWV
23|  WXYZABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBAZYXW
24| XYZABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBAZYX
25|YZABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBAZY