题目:传送门。(需要下载PDF)

题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式。

题解:比如300-456就改成300-4+56,遇到二位数以上的减数的情况就变成-首位+剩下的,这样会使得表达式值最大。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
string c;
int main()
{
    freopen("easy.in","r",stdin);
    freopen("easy.out","w",stdout);
    cin>>c;
    int len=c.length();
    bool flag=0;
    for(int i=0;i<len;i++)
    {
        if(c[i]=='-'||c[i]=='+') flag=0;
        if(flag) putchar('+');
        if(c[i]!='0') flag=0;
        if(c[i-1]=='-'&&i>=1) flag=1;
        putchar(c[i]);
    }
    puts("");
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2021-11-05
  • 2021-08-08
  • 2021-09-08
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2021-11-04
  • 2021-05-24
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案