Description
给出一个只由加减法的合法表达式,要求通过适当加一些加减运算符使得该表达式的值尽可能大,输出值最大的表达式,要求加完运算符后表达式仍合法,即数字没有前导零,没有负数,两个数字之间只能有一个运算符,第一个数字之前和最后一个数字之后不能有运算符
Input
给出一个长度不超过 1000 的表达式
Output
输出值最大的表达式
Sample Input
10+20-30
Sample Output
10+20-3+0
Solution
加入减法显然不优,只有通过把原表达式中的减号后面数字变得尽可能小并且能够多加数字才能增加整个表达式的值,例如原表示式要减去
x
,现在只减
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1001;
char s[maxn];
int main()
{
freopen("easy.in","r",stdin);
freopen("easy.out","w",stdout);
while(~scanf("%s",s))
{
int n=strlen(s);
for(int i=0;i<n;)
if(s[i]=='-')
{
printf("-%c",s[++i]);
i++;
if(!isdigit(s[i]))continue;
while(i<n&&s[i]=='0')printf("+0"),i++;
if(i==n)break;
if(isdigit(s[i]))printf("+%c",s[i++]);
}
else printf("%c",s[i++]);
printf("\n");
}
return 0;
}