首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【zzuliOJ】1913 - 小火山的计算能力(模拟)

【zzuliOJ】1913 - 小火山的计算能力(模拟)

作者头像
FishWang
发布2025-08-27 10:07:44
发布2025-08-27 10:07:44
8700
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

1913: 小火山的计算能力

Time Limit: 1 Sec Memory Limit: 128 MB Submit: 442 Solved: 105 Submit Status Web Board

Description

别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来。

Input

首先是一个t(1<=20)表示测试组数。然后一个表达式,表达式长度不超过200,只有加法和减法,并且保证第一个字符不会是运算符号,最终结果小于2^63-1。

Output

输出运算结果。

Sample Input

2

1+1

2+1-1

Sample Output

2

2

HINT

Source

zzuli

模拟计算机,只有加减,用longlong存。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
    int u;
    char a[211];
    scanf ("%d",&u);
    LL ans;
    LL num;
    while (u--)
    {
        scanf ("%s",a);
        int l = strlen(a);
        ans = 0;
        int i,j;
        for (i = 0 ; i < l ; i++)
        {
            if (a[i] == '+' || a[i] == '-')
                break;
            ans = ans * 10 + (a[i] - '0');
        }
        for ( ; i < l ; i++)
        {
            num = 0;
            for (j = i + 1 ; j < l ; j++)
            {
                if (a[j] == '+' || a[j] == '-')
                    break;
                num = num * 10 + (a[j] - '0');
            }
            if (a[i] == '+')
                ans += num;
            else
                ans -= num;
            if (j == l)
                break;
            else
                i = j-1;
        }
        printf ("%lld\n",ans);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1913: 小火山的计算能力
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档