输入样例:
2
1
2
2
1 1
一下这个死循环了
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int T, n, a[N];
bool vis[N];
bool check() {
int sum;
for(int i = 1; i <= n; i++) sum += vis[i];
return (sum != n);
}
int main() {
cin >> T;
while(T--) {
memset(vis, 0, sizeof vis);
vis[0] = true;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
int sum[2], old = 0;
bool book = 0;
while(check()) {
for(int i = 1; i <= n; i++)
if(!vis[i]) {
sum[book]++;
book ^= 1;
swap(vis[i], vis[old]);
if(!a[old]) vis[old] = true;
a[i]--;
old = i;
break;
}
}
cout << (book ? "T" : "HL") << endl;
}
return 0;
}
然后加了个 cout << "";,竟然不死循环了!
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int T, n, a[N];
bool vis[N];
bool check() {
int sum;
for(int i = 1; i <= n; i++) sum += vis[i];
return (sum != n);
}
int main() {
cin >> T;
while(T--) {
memset(vis, 0, sizeof vis);
vis[0] = true;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
int sum[2], old = 0;
bool book = 0;
while(check()) {
for(int i = 1; i <= n; i++)
if(!vis[i]) {
sum[book]++;
book ^= 1;
swap(vis[i], vis[old]);
if(!a[old]) vis[old] = true;
a[i]--;
old = i;
cout << "";
break;
}
}
cout << (book ? "T" : "HL") << endl;
}
return 0;
}