C*++语言和C++语言非常相似,然而C*++的程序有时会出现意想不到的结果。比如像这样的算术表达式:
表达式=基本式 / 表达式+基本式 / 表达式-基本式
基本式=增量 / 系数*增量
增量=a++ / ++a
系数=0/1/2/……/1000
如“5*a++-3*++a+a++”是合法的C*++表达式。
计算这样的表达式的值的方法:首先是每个基本式进行计算,然后按照正常的算术运算法则计算。如果一个基本式包含“a++”,则先进行乘法运算再使变量a权值+1;如果一个基本式包含“++a”,则先使变量a权值+1再进行乘法运算。
然而基本式可以按任意顺序计算,这就是为什么计算结果是完全无法预料的。
你的任务就是去找到最大的可能结果。
第一行,一个整数n,表示变量a的初始值。
第二行,一个合法的C*++表达式。
共一行,一个整数ans,表示最大可能结果。
From weixinding@CSDN
Code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool flag;
int a,n,i,j,k,x,o,ans;
int w[10011],tot;
char s[100011];
int main()
{
scanf("%d\n%s",&a,s+1);
n=strlen(s+1);
if (s[1]!='-')
{
for (i=n+1;i>1;i--) s[i]=s[i-1];
s[1]='+';
n++;
}
for (i=1;i<=n;i+=3)
{
if (s[i++]=='+') k=1;
else k=-1;
j=0;
while (i<=n&&'0'<=s[i]&&s[i]<='9') (j*=10)+=s[i++]-48;
if (s[i]=='*') i++;
else j=1;
w[++tot]=(k*=j);
ans+=(a-(s[i]=='a'))*k;
}
sort(w+1,w+tot+1);
for (i=1;i<=tot;i++) ans+=i*w[i];
cout<<ans;
return 0;
}