Gym 100801E Easy Arithmetic (思维题)

2023-03-15,,

题目:传送门。(需要下载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=;
for(int i=;i<len;i++)
{
if(c[i]=='-'||c[i]=='+') flag=;
if(flag) putchar('+');
if(c[i]!='') flag=;
if(c[i-]=='-'&&i>=) flag=;
putchar(c[i]);
}
puts("");
return ;
}

Gym 100801E Easy Arithmetic (思维题)的相关教程结束。

《Gym 100801E Easy Arithmetic (思维题).doc》

下载本文的Word格式文档,以方便收藏与打印。