Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 字母三角形_C_Loops_Char - Fatal编程技术网

C 字母三角形

C 字母三角形,c,loops,char,C,Loops,Char,我已经挣扎了几个小时,仍然无法解决这个问题。 以下是我在网上找到的答案: int main(){ int ch=65; int i,j,k,m; system("cls"); for(i=1;i<=5;i++) { for(j=5;j>=i;j--) printf(" "); for(k=1;k<

我已经挣扎了几个小时,仍然无法解决这个问题。 以下是我在网上找到的答案:

int main(){  
  int ch=65;    
    int i,j,k,m;    
  system("cls");  
    for(i=1;i<=5;i++)    
    {    
        for(j=5;j>=i;j--)    
            printf(" ");    
        for(k=1;k<=i;k++)    
            printf("%c",ch++);    
            ch--;    
        for(m=1;m<i;m++)    
            printf("%c",--ch);    
        printf("\n");    
        ch=65;    
    }    
return 0;  
}  
我的代码:

   CCCB
  CCCB
 CCCB

谢谢大家!

您的函数毫无理由地非常复杂

void print(int rows)
{
    for(int row = 1; row <= rows;row++)
    {
        int ch;
        for(int space = row; space < rows; space++) printf(" ");
        for(ch = 0; ch < row; ch++) printf("%c", 'A' + ch);
        while(--ch) printf("%c", 'A' + ch - 1);
        printf("\n");
    }
}

void printTpLetter(char ch)
{
    print(ch - 'A' + 1);
}

int main(void)
{
    printTpLetter('E');
    print(3);
}
void打印(int行)
{

对于(int row=1;row Thank you!),我确实按照您所说的修改了代码,现在逻辑更加清晰了。我试图将所有任务组合在一起,结果,我的程序变得非常复杂。
   CCCB
  CCCB
 CCCB
void print(int rows)
{
    for(int row = 1; row <= rows;row++)
    {
        int ch;
        for(int space = row; space < rows; space++) printf(" ");
        for(ch = 0; ch < row; ch++) printf("%c", 'A' + ch);
        while(--ch) printf("%c", 'A' + ch - 1);
        printf("\n");
    }
}

void printTpLetter(char ch)
{
    print(ch - 'A' + 1);
}

int main(void)
{
    printTpLetter('E');
    print(3);
}