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

题目背景
蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白。

题目描述
好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式。显然成功就在眼前了。可是他脑细胞和RP已经消耗殆尽,所以这个重要的任务就交给你们啦。

输入格式
一行,包含一个字符串形式的加减法多项式(每一项数字范围为0-32767)。

输出格式
一个整数,为计算所得的结果(保证计算结果不会超过长整型范围)。

输入输出样例
输入 #1

1+2-3

输出 #1

0

说明/提示
字符串长度不超过60000

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;

int ans;
int c;
int main()
{
    while(cin>>c)
    	ans+=c;
    cout<<ans;
    return 0;
}

====================================================

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;

int main(){
    int ans;
    cin>>ans;
    int cache;
    char c;
    while(1){
        if(scanf("%c",&c)==EOF){
            break;
        }
        scanf("%d",&cache);
        if(c=='+') ans+=cache;
        if(c=='-') ans-=cache;
    }
    printf("%d",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值