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

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

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

问题

  1. static int count = 0;
  2. public static void main(String[] args) {
  3. Scanner sc = new Scanner(System.in);
  4. count = 0;
  5. int n = sc.nextInt();
  6. int arr[][] = new int[n][n];
  7. for (int i = 0; i < n; i++) {
  8. for (int j = 0; j < n; j++) {
  9. arr[i][j] = sc.nextInt();
  10. }
  11. }
  12. for (int i = 0; i < n ; i++) {
  13. row(arr, n, i);
  14. column(arr, n, i);
  15. }
  16. System.out.println(count);
  17. for (int i = 0; i < n; i++) {
  18. for (int j = 0; j < n; j++) {
  19. System.out.print(arr[i][j] + " ");
  20. }
  21. }
  22. }
  23. static void row(int arr[][], int n, int row) {
  24. int x, y, min, yHold = 0;
  25. for (x = 0; x < n; x++) {
  26. boolean yes = false;
  27. min = arr[row][x];
  28. for (y = row; y < n; y++) {
  29. if (arr[y][x] < min) {
  30. min = arr[y][x];
  31. yHold = y;
  32. yes = !yes; // Here is the change you mentioned
  33. }
  34. }
  35. if (yes) {
  36. int temp = arr[row][x];
  37. arr[row][x] = min;
  38. arr[yHold][x] = temp;
  39. count++;
  40. }
  41. }

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

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

英文:
  1. static int count = 0;
  2. public static void main(String[] args) {
  3. Scanner sc = new Scanner(System.in);
  4. count = 0;
  5. int n = sc.nextInt();
  6. int arr[][] = new int[n][n];
  7. for (int i = 0; i &lt; n; i++) {
  8. for (int j = 0; j &lt; n; j++) {
  9. arr[i][j] = sc.nextInt();
  10. }
  11. }
  12. for (int i = 0; i &lt; n ; i++) {
  13. row(arr, n, i);
  14. column(arr, n, i);
  15. }
  16. System.out.println(count);
  17. for (int i = 0; i &lt; n; i++) {
  18. for (int j = 0; j &lt; n; j++) {
  19. System.out.print(arr[i][j] + &quot; &quot;);
  20. }
  21. }
  22. }
  23. static void row(int arr[][], int n, int row) {
  24. int x, y, min, yHold = 0;
  25. for (x = 0; x &lt; n; x++) {
  26. boolean yes = false;
  27. min = arr[row][x];
  28. for (y = row; y &lt; n; y++) {
  29. if (arr[y][x] &lt; min) {
  30. min = arr[y][x];
  31. yHold = y;
  32. yes = true;
  33. }
  34. }
  35. if (yes) {
  36. int temp = arr[row][x];
  37. arr[row][x] = min;
  38. arr[yHold][x] = temp;
  39. count++;
  40. }
  41. }

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 时,它会在真和假之间来回切换。

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

输出:

英文:

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.

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

prints

  1. true
  2. false
  3. true
  4. false
  5. true
  6. false
  7. true
  8. false
  9. true
  10. false
  11. </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:

确定