英文:
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 < 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++;
}
}
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
会在 true
和 false
之间不断翻转。
英文:
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 < 10; i++) {
System.out.println(yes);
yes =! yes;
}
prints
true
false
true
false
true
false
true
false
true
false
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论