看了一些网上人写的博客,不理解这种简单题为什么要写这么复杂,我来写一手简单的代码
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#define ls x<<1
#define rs x<<1|1
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e6+10;
const int MOD=9901;
const int mod = 1e9+7;
const double PI=3.14;
int lowbit(int x){return x&-x;}
ll gcd(ll x, ll y){ return y == 0 ? x : gcd(y, x%y); }
ll lcm(ll x, ll y){ return x / gcd(x, y)*y; }
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b,ll p){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % p; b >>= 1; t = (t*t) % p; }return r; }
priority_queue<int, vector<int>, greater<int> > q;
int main()
{
string str;
while(getline(cin,str))
{
int len=str.length();
for(int i=0;i<len;i++)
{
if(str[i]==' ')str.erase(i,1),i--;//先把空格去掉
}
string s;
if(isdigit(str[0]))//如果第一个是正数,在前面预处理加个+号
{
s="+"+str;
}
else s=str;
//cout<<s<<endl;
int sum=0;
int la=0;
for(int i=0;i<s.length();)
{
if(s[i]=='+')//遇见加号,读后面的数字
{
i++;
la=0;
while(isdigit(s[i]))
{
la=la*10+s[i]-'0';
i++;
}
sum+=la;
}
else{//遇见减号,读后面的数字
i++;
la=0;
while(isdigit(s[i]))
{
la=la*10+s[i]-'0';
i++;
}
sum-=la;
}
}
cout<<sum<<endl;
}
return 0;
}
水题