以下是AC代码:
#include<bits/stdc++.h>
#define int long long
#define QWQ cin.tie(0)->sync_with_stdio(false);
using namespace std;
signed main(){
QWQ
int t, n;
cin >> t;
auto solve = [&](){
cin >> n;
vector<int> v(n), used(n + 1, false);
for(auto &i : v) cin >> i;
for(auto &i : v){
int x = i;
while(x > n or used[x]) x /= 2;
if(x > 0){
used[x] = true;
}
else{
cout << "NO\n";
return;
}
}
cout << "YES\n";
};
while(t--) solve();
return 0;
}
然后我对代码做了个小改动(很小很小),令我惊讶的事情发生了:
以下注释是改动的地方:
#include<bits/stdc++.h>
#define int long long
#define QWQ cin.tie(0)->sync_with_stdio(false);
using namespace std;
signed main(){
QWQ
int t, n;
cin >> t;
auto solve = [&](){
cin >> n;
vector<int> v(n), used(n + 1, false);
for(auto &i : v) cin >> i;
for(auto &i : v){
int x = i;
while(used[x] or x > n) x /= 2; //就是这句,我只是把'or'两边换了下位置,就WA了!!??
if(x > 0){
used[x] = true;
}
else{
cout << "NO\n";
return;
}
}
cout << "YES\n";
};
while(t--) solve();
return 0;
}
报错信息:
Probably, the solution is executed with error 'out of bounds or other memory error' on the line 17(line 17就是我改动的那一句)
综上所述,这就是我感到奇怪的点:while括号里面是如何执行条件的呢?是不是有暗藏什么玄机呢?为什么更换下or两边条件的位置就会发生Runtime error?