zoukankan      html  css  js  c++  java
  • 【题解】「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;
    }
    
  • 相关阅读:
    常用正则表达式
    C语言的指针与二维数组
    【原创】datalist实现简单分页功能
    【原创】datalist的页脚访问和控制
    [原创]手动删除顽固病毒总结
    [zz]复杂指针解析
    极度郁闷的一次电脑维修经历
    武汉城市地铁规划图
    [转]objc_msgSend 的 ARM 汇编分析
    [转]Cydia and XCode Local App Testing
  • 原文地址:https://www.cnblogs.com/-TNT-/p/13205238.html
Copyright © 2011-2022 走看看