Loading

【题解】Codeforces[CF1373B] 01 Game

这题好水,就是简单的模拟+字符串。

\(\sf Translation\)

给定一个 \(01\) 串,如果 \(0\) 出现的次数和 \(1\) 出现的次数的最小值是奇数,输出 DA ,否则输出 NET

多测。

\(\sf Solution\)

法一

简单模拟+字符串,如果你是刚刚学字符串的萌新,推荐先看看 这题,这两题类似,都是统计一个字符串里面的字符的情况。

那么我们可以定义两个变量分别存储 \(0\) 的出现次数和 \(1\) 的出现次数。

\(\sf Code\)

/*
  Problem:CF1373B
  Date:28/06/20 21:29
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#define line cout << endl
using namespace std;
int t;
int main () {
	cin >> t;
	int _1, _0;
	while (t--) {
		string s;
		cin >> s;
		int len = s.length();
		for (int i = 0; i < len; i++) {
			if (s[i] == '1') _1++;//如果当前字符是1 
			else _0++;
		}
		cout << (min (_1, _0) % 2 == 0 ? "NET" : "DA") << endl;//取最小值/判断奇偶/输出 
		_1 = 0, _0 = 0;//清零 
	}
	return 0;
}

法二

利用 c++ 的 STL 中的 count 函数。

count 的用法:

count 共有 3 个参数:

count(begin, end, c);

其中 begin 代表字符串的起始位置,end 代表终止位置,c 代表要统计的字符。

那在这道题里面,我们就可以用 count 函数统计 \(0\)\(1\) 的个数。

\(\sf Code\)

/*
  Problem:CF1373B
  Date:28/06/20 21:29
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#define line cout << endl
using namespace std;
int t;
int main () {
	cin >> t;
	int _1, _0;
	while (t--) {
		string s;
		cin >> s;
		cout << (min (count (s.begin (), s.end (), '0'), count (s.begin (), s.end (), '1')) % 2 == 0 ? "NET" : "DA") << endl;//统计/取最小值/判断奇偶/输出 
	}
	return 0;
}
posted @ 2020-06-28 21:56  AgrumeStly  阅读(171)  评论(0编辑  收藏  举报