为什么使用 “不” 运算符得到不同的结果,但使用 “=” 得到正确答案?

huangapple go评论71阅读模式
英文:

Why is not operator giving different result but using = giving correct answer

问题

 static int count = 0;

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    count = 0;
    int n = sc.nextInt();
    int arr[][] = new int[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        arr[i][j] = sc.nextInt();
      }
    }
    for (int i = 0; i < n ; i++) {
      row(arr, n, i);
      column(arr, n, i);
    }
    System.out.println(count);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.print(arr[i][j] + " ");
      }
    }
  }

  static void row(int arr[][], int n, int row) {
    int x, y, min, yHold = 0;
    
    for (x = 0; x < n; x++) {
      boolean yes = false;
      min = arr[row][x];
      
      for (y = row; y < n; y++) {
        if (arr[y][x] < min) {
          min = arr[y][x];
          yHold = y;
          yes = !yes;  // Here is the change you mentioned
        }
      }
      if (yes) {
        int temp = arr[row][x];
        arr[row][x] = min;
        arr[yHold][x] = temp;
        count++;
      }
    }

如果我将yes=true替换为yes=!yes,输出会变得不同。请帮忙,我似乎无法理解这个问题。函数row所处理的问题部分是:

如果我们在第i行,则必须逐列从0到n−1处理该行。对于任何第j列,将A[i][j]与索引为i到n−1的行中在第j列上存在的所有元素的最小值进行交换。

英文:
 static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
count = 0;
int n = sc.nextInt();
int arr[][] = new int[n][n];
for (int i = 0; i &lt; n; i++) {
for (int j = 0; j &lt; n; j++) {
arr[i][j] = sc.nextInt();
}
}
for (int i = 0; i &lt; n ; i++) {
row(arr, n, i);
column(arr, n, i);
}
System.out.println(count);
for (int i = 0; i &lt; n; i++) {
for (int j = 0; j &lt; n; j++) {
System.out.print(arr[i][j] + &quot; &quot;);
}
}
}
static void row(int arr[][], int n, int row) {
int x, y, min, yHold = 0;
for (x = 0; x &lt; n; x++) {
boolean yes = false;
min = arr[row][x];
for (y = row; y &lt; n; y++) {
if (arr[y][x] &lt; min) {
min = arr[y][x];
yHold = y;
yes = true;
}
}
if (yes) {
int temp = arr[row][x];
arr[row][x] = min;
arr[yHold][x] = temp;
count++;
}
}

If I replace yes=true with yes=!yes, the output becomes different. Please help, can't seem to figure it out.
The part of the problem the function row is taking are of is:

> If we are at ith row, then we have to work with each column at a time
> from 0 to n−1 of this row. For any jth column, swap A[i][j] with the
> minimum of all the elements which are present in a column with index j
> and rows from indices i to n−1.

答案1

得分: 3

问题在于该语句可以被执行多次。在 yes = true 的情况下,结果将永远是 yes 变为 true。但是使用 yes = !yes,结果是 yes 会在 truefalse 之间不断翻转。

英文:

The problem is that the statement can be executed multiple times. In the case of yes = true, the result will always be that yes becomes true. But with yes = !yes, the result is that yes will flip flop between true and false.

答案2

得分: 3

以下是您要翻译的内容:

当您说 yes = true 时,它将始终为真。但是当您说 yes = !yes 时,它会在真和假之间来回切换。

boolean yes = true;
for (int i = i < 10; i++) {
 System.out.println(yes);   
 yes = !yes;
}

输出:

英文:

When you say yes = true, it is always going to be true. But when you say, yes = !yes it will flip back and forth between true and false.

boolean yes = true;
for (int i = i &lt; 10; i++) {
System.out.println(yes);   
yes =! yes;
}

prints

true
false
true
false
true
false
true
false
true
false
</details>

huangapple
  • 本文由 发表于 2020年5月29日 10:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/62077913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定