Algorithm Training c*++ calculations

Source: Internet
Author: User
Tags arithmetic

Algorithm training c*++ Calculations time limit: 2.0s memory limit: 64.0MBThe problem description c*++ language is very similar to the C + + language, while c*++ programs sometimes have unexpected results. For example, an arithmetic expression like this:
expression = basic/Expression + basic/expression-basic
Basic = increment/factor * increment
Incremental =a++/++a
Coefficient =0/1/2/....../1000
such as "5*a++-3*++a+a++" is a valid c*++ expression.
The method for calculating the value of such an expression is first calculated for each primitive and then computed according to the normal arithmetic algorithm. If a primitive contains "a++", then multiplication is performed before the variable a weights +1, and if a primitive contains "++a", then the variable a weights +1 is multiplied.
However, the basic formula can be calculated in any order, which is why the calculation results are completely unpredictable.
Your task is to find the maximum possible outcome.

The first line, an integer n, represents the initial value of the variable A.
The second line, a valid c*++ expression.

A total of one line, an integer ans, that represents the maximum possible result. Format Input 1:
1
5*a++-3*++a+a++
Input 2:
3
A+++++a output Format 1:
11
Output 2:
8 Data size and conventions for 20% of the data, the expression length is <=20.
Another 20% of the data is available to meet n>=0.
For 100% of the data, -1000<=n<=1000, the expression length is <=10000.
Note that there may be a minus sign at the beginning of an expression!
1#include <stdio.h>2#include <string.h>3 #defineMAXSIZE 100114 /*The idea is to sort the coefficients of each ++a or a++, followed by5 A from the beginning of the increment of the form to add, you need to pay attention to the case of ++a, need to advance the amount of an additional time*/6 intN, ans, K, Coe, Len, outcome, c[maxsize];7 CharE[maxsize], s[maxsize];8 9 voidSortintx)Ten { One     intI, J, T; A  -      for(i =1; i < x; i + +){ -          for(j = i+1; J >1; j--){ the  -             if(C[j] < c[j-1]){ -t = c[j-1]; -c[j-1] =C[j]; +C[J] =T; -}Else{ +                  Break; A             } at         } -     } - } -  - intCalculate () - { in     intI, J; -  toK = outcome =0; + strcpy (S, e); -Len =strlen (s); the  *     if(s[0] !='-'){ $ Panax Notoginseng          for(i = len +1; i >0; I--){ -S[i] = s[i-1]; the         } +  As[0] ='+'; theLen + +; +     } -  $      for(i =0; i < Len; i + =3){ $  -         if(S[i] = ='+'){ -Coe =1; the}Else{ -Coe =-1;Wuyi         } the  -i + +; Wuj =0; -          while('0'<= S[i] && s[i] <='9'){ AboutJ *=Ten; $J + = s[i++]-'0'; -         } -  -         if(S[i] = ='*'){ Ai + +; +}Else{ thej =1; -         } $  theCoe *=J; theC[++K] =Coe; theOutcome + = (n (s[i) = ='a')) *Coe; the     } -  in sort (k); the      for(i =1; I <= K; i + +){ theOutcome + = i *C[i]; About     } the  the     returnoutcome; the } +  - intMain () the {Bayiscanf"%d%s", &N, e); the  theAns =calculate (); -  -printf"%d\n", ans); the  the     return 0; the}

Algorithm Training c*++ calculations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.