[ABC033C] 数式の書き換え 题解

· · 题解

一道简单的计数题。很显然,由于题目的特殊条件,想让最终的和为 0,需要将算式中的每一项都变为 0

因为只包含加号和乘号,而一个数乘以 00,所以对于每一项分类讨论:若其中有 0 的乘数,则不需要改动,否则将其中一个乘数变为 0 即可。

放代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
  ios::sync_with_stdio(false);
  string s; int c=0,x=1; cin>>s;
  for(char i:s)
    switch(i){
      case '+':c+=x,x=1; break;
      case '0':x=0;
    }
  cout<<c+x<<endl;
  return 0;
}