Problem E. Easy Arithmetic

本文介绍了一种算法,用于处理包含加减运算的算术表达式,通过在合适的位置添加加号,使最终表达式的计算结果达到最大值。算法首先读取原始表达式,然后检查每个减号后的数字,如果数字超过一位,则在减号和数字间插入加号,以此来增加表达式的总值。

Problem E. Easy Arithmetic
Input file: easy.in
Output file: easy.out
Time limit: 2 seconds
Memory limit: 256 megabytes
Eva is a third-grade elementary school student. She has just learned how to perform addition and
subtraction of arbitrary-precision integers. Her homework is to evaluate some expressions. It is boring,
so she decided to add a little trick to the homework. Eva wants to add some plus and minus signs to the
expression to make its value as large as possible.
Input
The single line of the input file contains the original arithmetic expression. It contains only digits, plus
(‘+’) and minus (‘-’) signs.
The original expression is correct, that is:
• numbers have no leading zeroes;
• there are no two consecutive signs;
• the last character of the expression is a digit.
The length of the original expression does not exceed 1000 characters.
Output
Output a single line — the original expression with some plus and minus signs added. Output expression
must satisfy the same correctness constraints as the original one. Its value must be as large as possible.
Examples
easy.in easy.out
10+20-30 10+20-3+0
-3-4-1 -3-4-1
+10 +10

题目给的已经是正确的表达式,现要求它的值最大,也就是在遇到负号时,如果负号后面的数字超过十位,就往里面添加加号。
-3010          -3+0+10
-30010        -3+0+0+10
-3330          -3+330
    /**/
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        freopen("easy.in","r",stdin);
        freopen("easy.out","w",stdout);
        int i,j,len;
        int a[1010]={0};
        char s[1010];
        scanf("%s",s);
        len=strlen(s);
        for(i=0;i<len;i++)
        {
            if(s[i]=='-')
            {
                if(i+2<len&&s[i+2]!='+'&&s[i+2]!='-')
                {
                    for(j=i+2;j<len;j++)
                    {
                        if(s[j]=='0')
                            a[j]=1;
                        else if(s[j]>='1'&&s[j]<='9')
                        {
                            a[j]=1;
                            break;
                        }
                        else
                            break;
                    }
                }
            }
        }
        for(i=0;i<len;i++)
        {
            if(a[i])
                printf("+");
            printf("%c",s[i]);
        }
        printf("\n");
        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值