为什么运算符不给出不同的结果而是使用 = 给出正确答案
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 = true;
}
}
if (yes) {
int temp = arr[row][x];
arr[row][x] = min;
arr[yHold][x] = temp;
count++;
}
}
如果我将 yes=true
替换为 yes=!yes
,输出会有所不同。请帮忙,似乎无法弄清楚。
函数行所涉及的问题部分是:
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.
问题是语句可以执行多次。在 yes = true
的情况下,结果总是 yes
变成 true
。但是对于 yes = !yes
,结果是 yes
将在 true
和 false
之间翻转。
当您说 yes = true
时,它总是正确的。但是当你说 yes = !yes
它会在 true 和 false 之间来回翻转。
boolean yes = true;
for (int i = i < 10; i++) {
System.out.println(yes);
yes =! yes;
}
打印
true
false
true
false
true
false
true
false
true
false
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 = true;
}
}
if (yes) {
int temp = arr[row][x];
arr[row][x] = min;
arr[yHold][x] = temp;
count++;
}
}
如果我将 yes=true
替换为 yes=!yes
,输出会有所不同。请帮忙,似乎无法弄清楚。
函数行所涉及的问题部分是:
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.
问题是语句可以执行多次。在 yes = true
的情况下,结果总是 yes
变成 true
。但是对于 yes = !yes
,结果是 yes
将在 true
和 false
之间翻转。
当您说 yes = true
时,它总是正确的。但是当你说 yes = !yes
它会在 true 和 false 之间来回翻转。
boolean yes = true;
for (int i = i < 10; i++) {
System.out.println(yes);
yes =! yes;
}
打印
true
false
true
false
true
false
true
false
true
false