根据以下代码的逻辑再生成一个能通过查重且正确的c++代码:#include <iostream> #include <vector> using namespace std; const int MAX_N = 110; int s[MAX_N]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } //从编号小的开始枚举所有可能情况 for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { vector<int> liars; bool isWolf[MAX_N] = { 0 }; //假设i,j为狼人 isWolf[i] = isWolf[j] = true; //枚举所有人的发言 for (int k = 1; k <= n; k++) { //如果当前发言人与假设矛盾,则说明此人说谎 if ((isWolf[abs(s[k])] && s[k] > 0) || (!isWolf[abs(s[k])] && s[k] < 0)) { liars.push_back(k); } } //如果说谎的人 == 2 并且说谎者只有一个狼人 if (liars.size() == 2 && isWolf[liars[0]] != isWolf[liars[1]]) { cout << i << " " << j << endl; return 0; } } } puts("No Solution"); return 0; }
时间: 2024-04-18 16:23:25 浏览: 72
用vector解决约瑟夫环.txt
#include <iostream>
#include <vector>
using namespace std;
const int_N = 110;
int s[MAX_N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
vector<int> liars;
bool isWolf[MAX_N] = { 0 };
isWolf[i] = isWolf[j] = true;
for (int k = 1; k <= n; k++) {
if ((isWolf[abs(s[k])] && s[k] > 0) ||
(!isWolf[abs(s[k])] && s[k] < 0)) {
liars.push_back(k);
}
}
if (liars.size() == 2 && isWolf[liars[0]] != isWolf[liars[1]]) {
cout << i << " " << j << endl;
return 0;
}
}
}
puts("No Solution");
return 0;
}
阅读全文