原题链接: https://codeforces.com/contest/1419/problems
测试样例
input
4
1
2
1
3
3
102
4
2069
output
2
1
1
2
Note
In the first match no one can make a turn, the only digit left is 2, it’s even, so Breach wins.
In the second match the only digit left is 3, it’s odd, so Raze wins.
In the third match Raze can mark the last digit, after that Breach can only mark 0. 1 will be the last digit left, it’s odd, so Raze wins.
In the fourth match no matter how Raze plays, Breach can mark 9, and in the end there will be digit 0. It’s even, so Breach wins.
题意: 有一个整数字符串,现在Raze和Breach进行游戏:
- Raze可以对奇位置上的数字标记,不可以对偶数位置上的标记
- Breach则相反
- Raze先开始标记
- 当剩余标记数只剩下一个了的时候,若此数字为奇数,则Raze赢,否则Breach赢。
解题思路: 认真读题,仔细理解就好做了。如果n为奇数,那么说明奇数位数目多于偶数位数目,最后剩下的数字由Raze决定。我们只要判断奇数位置上有无奇数。 如果n为偶数,说明奇偶数目相同,但Raze先开始,所以剩下的数字由Breach决定。我们只要判断偶数位置上有无偶数。 OK,则此题易解。
AC代码
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n;
string s;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n>>s;
bool flag=false;
if(n&1){
//如果n为奇数,那么说明奇数位数目多于偶数位数目,最后剩下的数字由Raze决定。
//我们只要判断奇数位置上有无奇数。
for(int i=0;i<n;i+=2){
if((s[i]-'0')&1){
flag=true;
break;
}
}
if(flag){
cout<<1<<endl;
}
else{
cout<<2<<endl;
}
}
else{
//如果n为偶数,说明奇偶数目相同,但Raze先开始,所以剩下的数字由Breach决定。
//我们只要判断偶数位置上有无偶数。
for(int i=1;i<n;i+=2){
if((s[i]-'0')%2==0){
flag=true;
break;
}
}
if(flag){
cout<<2<<endl;
}
else{
cout<<1<<endl;
}
}
}
}
return 0;
}