POJ 3480
john 必胜:
1,所有均为1且SG值为0;
2,有些堆大于1且SG值不为0。
#include"iostream" using namespace std; int main() { int k; cin>>k; while(k--) { int flag=1,n,i,s[50],ans=0; cin>>n; for(i=0;i<n;i++) { cin>>s[i]; if(s[i]!=1) flag=0; ans^=s[i]; } if(flag) { if(!ans) cout<<"John"<<endl; else cout<<"Brother"<<endl; continue; } if(ans) cout<<"John"<<endl; else cout<<"Brother"<<endl; } return 0; } 看到有人0ms,于是优化了一下,下面达到0MS. #include"iostream" #include"algorithm" using namespace std; int main() { int k; cin>>k; while(k--) { int n,i,s[50],ans=0; cin>>n; for(i=0;i<n;i++) { cin>>s[i]; ans^=s[i]; } sort(s,s+n); if(s[n-1]==1) { if(n%2) cout<<"Brother"<<endl; else cout<<"John"<<endl; } else if(ans) cout<<"John"<<endl; else cout<<"Brother"<<endl; } return 0; }