c - 我怎样才能再次这样做?在C中

标签 c arrays

#define G 10
int main(){
int grid[G][G],c,x;
for(c=1;c<=5;c++){
    for(x=1;x<=5;x++){
        if(c+x<=5)
            grid[c][x]=+1;
        else if(c+x>=7)
            grid[c][x]=-1;
        else
            grid[c][x]=0;

        printf("%2d\t ",grid[c][x]);
    }
    printf("\n");
}
getch();
return 0;
}

它的输出是 enter image description here 这是我真正想做的,但现在我需要让它看起来像 enter image description here

现在我不知道该怎么做,现在很伤我的头

最佳答案

我会这样做

for(c=0; c<5; c++) {     /* arrays start at 0, not 1. */
  for(x=0; x<5; x++) {   /* arrays start at 0, not 1. */
    if (c == x) {        /* looking at your output, the 0's occur when c == x */
      grid[c][x] = 0;
    } else if (c > x) {  /* the -1 when c > x */
      grid[c][x] = -1;
    } else {             /* obviously c > x */
      grid[c][x] = 1;
    }
    /* nothing else changed */
    printf("%2d\t ",grid[c][x]);
  }
  printf("\n");
}

关于c - 我怎样才能再次这样做?在C中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20561674/

相关文章:

javascript - 如果包含键值对,则返回数组中的对象

c - 在C中解析多行字符串

arrays - 是否可以找到数组中元组的索引?

c - 执行解释器文件时出错

c - 在 Windows 中跨重启访问相同的资源

arrays - 用 TArray<XXX> 替换 XXX 数组是否安全

javascript - 获取 javascript 数组中项目的单个属性

javascript - 如何从特定的另一个对象创建对象

c - 使用 DT_FILTER 从 DSO 隐藏符号

c++ - 缓存未命中压力测试 : stunning results. 。有什么解释吗?