Codeforces Round #310 (Div. 2)--A(简单题)

http://codeforces.com/problemset/problem/556/A

题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1。

题解:统计所有的0有多少个,1有多少个,最后答案就是两者不同的个数。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn=200010;
char num[maxn];
int main()
{
    int n,zero=0,one=0;
    scanf("%d",&n);
    scanf("%s",num);
    for(int i=0;i<n;i++){
        if(num[i]=='0') zero++;
        if(num[i]=='1') one++;
    }
    if(zero==one) printf("0
");
    else printf("%d
",zero>one?zero-one:one-zero);
    return 0;
}
原文地址:https://www.cnblogs.com/RRirring/p/4614433.html