当前位置: 移动技术网 > 移动技术>移动开发>IOS > Codeforces round #658

Codeforces round #658

2020年07月24日  | 移动技术网移动技术  | 我要评论

B
n堆石子,按顺序取,每次取正整数个,取到最后一个的获胜,问先手还是后手获胜。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int a[100010];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n,ans=0;
		cin>>n;
		bool f=0;
		for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]!=1&&ans==0)
            {
                ans=i;
                f=1;
            }
        }
        if(f)
        {
            if(ans%2) cout<<"First"<<endl;
            else cout<<"Second"<<endl;
        }
        else
        {
            if(n%2) cout<<"First"<<endl;
            else cout<<"Second"<<endl;
        }
	}
	return 0;
}

这题的关键是主动权掌握在谁手里,只剩一个的时候没得选,因此第一个可以从不是1的堆里选的人掌握主动权

C1
给出一串01字符,每次操作选中前 i 个,0和1互换且整体倒过来,问经过几次并且选择前几个可以得到目标字符串

#include<bits/stdc++.h>
using namespace std;
int main() 
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		string str1;
		string str2;
		cin >> str1 >> str2;
		int i, j;
		i = 0;
		j = n - 1;
		int flag = 1;
		for (i = 0; i < n; i++) {
			if (str1[i] != str2[i]) {
				flag = 0;
			}
		}		
		vector<int> ans;
		i = 0;
		j = n - 1;
		while (j >= 0) {
			int k = 1;
			if (str2[j] == str1[0]) {
				ans.push_back(1);
				k = 0;
			}
			for (k; k < str2.size() - i; k++) {
				if (str1[k] == '0') {
					str1[k] = '1';
				}
				else {
					str1[k] = '0';
				}
			}
			reverse(str1.begin(), str1.end() - i);
			ans.push_back(str2.size() - i);
			i++;
			j--;
		}
		cout << ans.size() ;
		for (i = 0; i < ans.size(); i++) {
			cout << ' ' << ans[i];
		}
		cout << endl;
	}
	return 0;
}

reverse是把字符串倒置

C2

#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		string str1;
		string str2;
		cin >> str1 >> str2;
		int i, j;
		i = 0;
		j = n - 1;
		int flag = 1;
		for (i = 0; i < n; i++) {
			if (str1[i] != str2[i]) {
				flag = 0;
			}
		}
		vector<int> idx;
		i = 0;
		j = n - 1;
		while (i <= j) {
			if (i != j) {
				idx.push_back(i);
				idx.push_back(j);
			}
			else {
				idx.push_back(i);
			}
			i++; j--;
		}
		vector<int> ans;
		j = n - 1;
		int cnt = 0;
		while (j >= 0) {
			if (str2[j] != (str1[idx[cnt] ]^ (cnt % 2))) {
				ans.push_back(j+1);
			}
			else {
				ans.push_back(1);
				ans.push_back(j+1);
			}
			cnt++;
			j--;
		}
		cout << ans.size() ;
		for (i = 0; i < ans.size(); i++) {
			cout << ' ' << ans[i];
		}
		cout << endl;
	}
	return 0;
}

本文地址:https://blog.csdn.net/qq_45792208/article/details/107526021

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网