https://www.luogu.com.cn/problem/P2788
#include<iostream>
using namespace std;
long long tot;
int tem = 0,f = 1;
int main()
{
string a;
cin >> a;
int len = a.size();
for(int i = 0;i < len;i++)
{
if(a[i] >= '0'&&a[i] <= '9')
tem=tem*10+(a[i]-'0');
else
{
tot+=f*tem;
tem = 0;
if(a[i] == '+')
f = 1;
else if(a[i] == '-')
f = -1;
else
{
break;
}
}
}
cout << tot+f*tem;
}
- 编程就像写文章,要理清解题的思路和逻辑
- 弄清楚符号f应该在哪里声明,在哪里更新
大佬
C++有一个不能算特性的特性。
这样读入的本质是cincincin将题目认为的+++运算和−-−运算当做了整数的正负号,又有a−b=a+(−b)a-b=a+(-b)a−b=a+(−b),所以答案是正确的。
#include<bits/stdc++.h>
using namespace std;
int ans,t;
int main()
{
while(cin>>t) ans+=t;//读入并计算
cout<<ans;//输出
return 0;
}
python是个好东西
print(input()) #直接计算输入表达式并输出结果