Printing out characters in an X pattern using for loops

后端 未结 4 1318
小蘑菇
小蘑菇 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:55

    A slight extension to one of the good solutions above for the cross ended up being a bit more than just crossing x's:

    #include 
    
    int main(){
        int n = 4 - 1;
        char ch[] = "x";
        int i = 0, dx = 1;
    
        printf ("\n  __\n  ||----------------------------\n");
        do {
            printf ("  %s %*s%*.*s %*c\n", "||", 4*i+1, ch, 8*(n-i), 8*(n-i), ch, 4*i+1, '|');
            if ((i += dx)==n)
                dx = -dx;
        } while (i>=0);
        printf ("  ||----------------------------\n");
        for (i = 0; i < 10; i++)
            printf ("  ||\n");
        printf ("------\n\n");
    
        return 0;
    }
    

    output:

    $ ./bin/flag
    
      __
      ||----------------------------
      || x                       x |
      ||     x               x     |
      ||         x       x         |
      ||             x             |
      ||         x       x         |
      ||     x               x     |
      || x                       x |
      ||----------------------------
      ||
      ||
      ||
      ||
      ||
      ||
      ||
      ||
      ||
      ||
    ------
    

提交回复
热议问题