N-Qiushi Big Brother. Time Limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
Qiushi Big Brother University physics hangs the branch, therefore in next semester of the first two weeks of one day to a tragic retake. In order not to give the school's excavator building to contribute, Qiushi decided to work hard in the holiday review. Of course, good computing power is also very necessary, after all, is involved in calculating how many points you can pass the question of the exam. Now he's given himself a whole bunch of long, arithmetic-shaped equations that only involve plastic, and then bury the results. He asked you to help in order to test his computational power.
Input
The first line is an integer t, which represents the total number of the equation.
The next line has an expression of not more than 10^6 in length, containing only positive integers and arithmetic symbols (' + ', '-', ' * ', '/').
Ensure that the input is valid.
Output
For each expression, output the corresponding result, which takes up one row.
Ensure that the operation and results are within a long long range.
Sample Input and output
Sample Input |
Sample Output |
212+5/4-14*5/3 |
126 |
Problem Solving Report
The subject: Obviously there is no special technique to use the stack to handle.
However, there are a few things can be improved, because there is no parentheses, and the data is valid, so we can only use a stack to implement, and once the scan processing off all the *,/,-number, and then left in the stack of the element operation is a +, note the use of long long, so that the problem solved.
#include <iostream>#include<cstring>using namespaceStd;typedefLong Longll;Const intMAXN = 1e6 + -; ll S[MAXN];CharTEMP[MAXN];intMainintargcChar*argv[]) { intCase,top; scanf ("%d%*c",&Case ); while(case--) {Top=0; Charch; LL Read=0; intOpe =-1; LL Sign=1; scanf ("%s", temp); intLen =strlen (temp); intpos =0; while(Pos <Len) {CH= temp[pos++]; if(ch = ='#'|| ch = =' ') Break; //printf ("%c\n", ch); if(Ch <='9'&& CH >='0') {Read*=Ten; Read+ = (ch-'0'); } Else{s[top+ +] = read*Sign ; Sign=1; //cout << read << Endl;Read =0; if(Ope = =2) {S[top-2] = s[top-1]*s[top-2]; Top--; } Else if(Ope = =3) {S[top-2] = s[top-2]/s[top-1]; Top--; } ope= -1; if(ch = ='-') Sign= -1; Else if(ch = ='*') Ope=2; Else if(ch = ='/') Ope=3; }} s[top+ +] = sign*Read; if(Ope = =2) {S[top-2] = s[top-1]*s[top-2]; Top--; } Else if(Ope = =3) {S[top-2] = s[top-2]/s[top-1]; Top--; } ll ans=0; for(inti =0; i < top; ++i) ans+=S[i]; printf ("%lld\n", ans); } return 0;}
Uestc_ Qiushi Big Brother UESTC Training for Data Structures<problem n>