字母模式大写字母金字塔C编程

时间:2016-11-03 23:03:58

标签: c

当我在C(通过Xcode)做一个字母金字塔时,我感到非常困难。我们必须输入相当于大写字母数的行数。这应该是它的样子:

输入行数:4

输出:

   A
  ABA
 ABCBA
ABCDCBA

仅限于最小值:4和最大值26.这是我迄今为止所管理的内容。提前谢谢你:

int main(){

    char letter = 'A';  //NOTE ! letter + 1 = 'B'
    int i, rows, space, k = 0;

    printf("Enter number of rows: \t");
        scanf("%i", &rows);



    while(rows < 4 || rows > 26){
        printf("\nError !\nMin : 4\tMax : 26\n");
            scanf("%i", &rows);

    }



    for(i = 1; i <= rows; i++,k =0){
        for(space = 1; space <= rows - i; space++){
            printf(" ");

        }

        for(; k!= 2* i-1; k++){
            printf("%c", letter);
        }

        for (char temp = letter + 1; temp <= letter; temp--){
            printf("%c", temp);
        }

        printf("\n");
    }



    return 0;
}

输出:

输入行数:4

   A
  AAA
 AAAAA
AAAAAAA

程序以退出代码结束:0

1 个答案:

答案 0 :(得分:0)

解决了:

#include <stdio.h>

int main(){

char letter = 'A', temp, temp1;  //NOTE ! letter + 1 = 'B'
int i, rows, space, k = 0;

printf("Enter number of rows: \t");
    scanf("%i", &rows);



while(rows < 4 || rows > 26){
    printf("\nError !\nMin : 4\tMax : 26\n");
        scanf("%i", &rows);

}

for(i = 1; i <= rows; i++,k =0){
    for(space = 1; space <= rows - i; space++){
        printf(" ");


    }

    temp = letter + i - 1;
    temp1 = temp;


    for(; k!= 2* i-1; k++){

        if(letter == temp){
            printf("%c", temp1);
            temp1--;
        }else{

        printf("%c", letter);
        letter++;
    }


    }

    letter = 'A';

    printf("\n");
}



return 0;

}