Printing out characters in an X pattern using for loops

后端 未结 4 1275
小蘑菇
小蘑菇 2021-01-21 14:05

I\'m trying to print this big X pattern:

 x       x
  x     x
   x   x
    x x
     x
    x x
   x   x
  x     x
 x       x

I can\'t figure out

4条回答
  •  佛祖请我去吃肉
    2021-01-21 14:43

    #include 
    
    int main(){
        int n = 5 - 1;
        char ch[] = "x";
        int i = 0, dx = 1;
    
        do{
            printf("%*s%*.*s\n", i+1, ch, 2*(n-i),2*(n-i), ch);
            if((i += dx)==n)
                dx = -dx;
        }while(i>=0);
        return 0;
    }
    

    for(;;){
        printf("%*s%*.*s\n", i+1, ch, 2*(n-i),2*(n-i), ch);
        if((i += dx)==n)
            dx = -dx;
        else if(i < 0)
            break;
    }
    

    int n = 5 - 1;
    char ch[] = "x";
    int i = 0;
    
    for(; i < n ; ++i){
        printf("%*s%*.*s\n", i+1, ch, 2*(n-i),2*(n-i), ch);
    }
    for(; i >=0 ; --i){
        printf("%*s%*.*s\n", i+1, ch, 2*(n-i),2*(n-i), ch);
    }
    

提交回复
热议问题