P2788 数学1(math1)- 加减算式

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()) #直接计算输入表达式并输出结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值