Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我的代码:

#include <stdio.h>
#include <math.h>

void block(int r, char a, char b);

int main(){
  block(2, '+', '-');
}

void block(int r, char a, char b){
  int i;
  char x = a;
  char y = b;
  for (i = 1; i <= r; i++){
    if ( i%2 == 1){
      for (i = 1; i <= r; i++){
        printf("%c", x);
      }
      for (i = 1; i <= r; i++){
        printf("%c", y);
      }
    } else
    {
      for (i = 1; i <= r; i++){
        printf("%c", x);
      }
      for (i = 1; i <= r; i++){
        printf("%c", y);
      }
    }
    printf("\n");
  }
  return;
}


预期的输出:

++--
--++


我得到的是:

++--


第一行之后没有任何显示。例如对于r = 5,它将正确打印第一行+++++ -----,但不会打印解决方案的其余行。

正确的解决方案:

+++++-----
-----+++++
+++++-----
-----+++++
+++++-----

最佳答案

感谢@rustyx

最终为我完成了窍门。

#include <stdio.h>
#include <math.h>

void block(int r, char a, char b);

int main(){
  block(5, '+', '-');
}

void block(int r, char a, char b){
  int i;
  char x = a;
  char y = b;
  for (i = 1; i <= r; i++){
    if ( i%2 == 1){
      int j;
      for (j = 1; j <= r; j++){
        printf("%c", x);
      }
      for (j = 1; j <= r; j++){
        printf("%c", y);
      }
    }
    else{
      int k;
      for (k = 1; k <= r; k++){
        printf("%c", y);
      }
      for (k = 1; k <= r; k++){
        printf("%c", x);
      }
    }
    printf("\n");
  }
  return;
}


得到这个结果:

+++++-----
-----+++++
+++++-----
-----+++++
+++++-----

07-28 11:30