1 条题解
-
0
C :
#include<stdio.h> int main() { char ch,b[1000100]; long q=0,sum=0; long long d[1000000]={0},s; while(scanf("%lld%c",&s,&ch)!=EOF) { d[q]=s%100000; if(ch=='*') b[q+1]='*'; if(b[q-1]=='*'&&(q-1)>=0) { d[q]=d[q]*d[q-2]; d[q]=d[q]%100000; b[q-1]=' '; } if(ch=='+') { sum=sum+d[q]; } q+=2; if(ch==10) { printf("%ld\n",(sum+d[q-2])%10000); q=0,sum=0; } } return 0; }
C++ :
#include <iostream> #include <cstdio> #include <stack> using namespace std; stack<unsigned long> num_stack; stack<char> oper_stack; const int READ_OPER = 0; const int READ_NUM = 1; const int READ_END = 2; int main() { int flag = READ_NUM; unsigned long t1,t2; char oper; num_stack.push(1); oper_stack.push('*'); while(1) { if(flag == READ_NUM) { if(scanf("%lu",&t1) != 1) { flag = READ_END; continue; } num_stack.push(t1%10000); flag = READ_OPER; } if(flag == READ_OPER) { if(scanf("%c",&oper) != 1 || (oper != '+' && oper != '*')) { flag = READ_END; continue; } if(oper_stack.top() == '*') { t1 = num_stack.top(); num_stack.pop(); t2 = num_stack.top(); num_stack.pop(); num_stack.push((t1*t2)%10000); //cout << t1 << 'v' << t2 << '-' << (t1*t2)%10000 << endl; oper_stack.pop(); } oper_stack.push(oper); flag = READ_NUM; } if(flag == READ_END) { if(oper_stack.empty()) break; oper = oper_stack.top(); oper_stack.pop(); t1 = num_stack.top(); num_stack.pop(); t2 = num_stack.top(); num_stack.pop(); if(oper == '+') { num_stack.push((t1+t2)%10000); //cout << t1 << 'v' << t2 << '-' << (t1+t2)%10000 << endl; } else { num_stack.push((t1*t2)%10000); //cout << t1 << 'v' << t2 << '-' << (t1*t2)%10000 << endl; } } } cout << num_stack.top() << endl; return 0; }
Pascal :
var sz:array [1..100001] of integer; fh:array [1..100001] of char; s:string; z:char; h,i,j:longint; begin while not eof do begin read(z); s:=''; while z in ['0'..'9'] do begin s:=s+z; read(z); end; i:=i+1; val(s,j); sz[i]:=j mod 10000; fh[i]:=z; end; for j:=1 to i do if fh[j]='*' then begin sz[j+1]:=sz[j]*sz[j+1] mod 10000; sz[j]:=0; end; for j:=1 to i do h:=h+sz[j]; write(h mod 10000); end.
Java :
import java.util.Scanner; import java.util.Stack; import java.math.BigInteger; public class Main { public static void main(String[] args) { ExpDealer ed = new ExpDealer(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String exp = scanner.nextLine(); if (exp.length() > 0) System.out.println(ed.calc(exp)); else System.out.println(); } } } class ExpDealer { private Stack<Character> stack1; private Stack<String> stack2; private Stack<BigInteger> result; public ExpDealer() { super(); this.stack1 = new Stack<Character>(); this.stack2 = new Stack<String>(); this.result = new Stack<BigInteger>(); } private boolean isOp(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '#' || c == '(' || c == ')'; } private boolean greater(char op1, char op2) { if (op2 == '#' || op2 == '(') { return true; } if (op1 == '*' || op1 == '/') { if (op2 == '+' || op2 == '-') return true; } return false; } private void reset() { stack1.clear(); stack2.clear(); result.clear(); } private Stack<String> predeal(String expression) { expression += '#'; int length = expression.length(); String tmp = ""; stack1.push('#'); for (int i = 0; i < length; i++) { char c = expression.charAt(i); if (c == ' ') { continue; } else if (isOp(c)) { if (tmp != "") { stack2.push(tmp); tmp = ""; } if (c == '(') { stack1.push(c); } else if (c == ')') { char t = stack1.peek(); while (t != '(') { stack2.push(t + ""); stack1.pop(); t = stack1.peek(); } stack1.pop(); } else { char t = stack1.peek(); while (!greater(c, t)) { stack2.push(t + ""); stack1.pop(); t = stack1.peek(); } stack1.push(c); } } else { tmp += c; } } while (!stack1.empty()) { char t = stack1.peek(); stack2.push(t + ""); stack1.pop(); } Stack<String> stack = new Stack<String>(); while (!stack2.empty()) { String t = stack2.pop(); stack.push(t); } return stack; } public BigInteger calc(String expression) { Stack<String> pre = predeal(expression); while (!pre.empty()) { String t = pre.pop(); if (t.equals("#")) { continue; } else if (isOp(t.charAt(0))) { BigInteger num2 = result.pop(); BigInteger num1 = result.pop(); if (t.equals("+")) { num1 = num1.add(num2); } else if (t.equals("-")) { num1 = num1.subtract(num2); } else if (t.equals("*")) { num1 = num1.multiply(num2); } else if (t.equals("/")) { num1 = num1.divide(num2); } result.push(num1); } else { result.push(new BigInteger(t, 10)); } } BigInteger ans = result.peek(); reset(); return ans.remainder(BigInteger.valueOf(10000)); } }
- 1
信息
- ID
- 312
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 10
- 标签
- 递交数
- 4
- 已通过
- 0
- 上传者