# 34 条题解

• @ 2016-08-13 11:17:06

人生苦短，这种题用py刷过。。
```python st = raw_input() print eval(st)%10000 ```

• @ 2017-09-04 11:53:18
``````//根据读入符号处理，若为+则入栈，若为*则继续操作，最后在统合
#include <bits/stdc++.h>
using namespace std;
int main()
{
stack<int> st;
long long num1, num2, ans = 0;
char ch;
cin >> num1;
while((ch = getchar()) != '\n')
{
switch(ch)
{
case '+':
st.push(num1);
cin >> num1;
break;
case '*':
cin >> num2;
num1 = num1 * num2;
break;
}
if(num1 > 10000)
num1 %= 10000;
}
st.push(num1);
while(!st.empty())
{
ans += st.top();
st.pop();
if(ans > 10000)
ans %= 10000;
}
cout << ans << endl;
return 0;
}
``````
• @ 2021-08-30 08:18:52
``````#include <bits/stdc++.h>
using namespace std;

stack <int> x;
int main()
{
int a,b; cin>>a;
char c;
int m=10000;
a=a%m;
x.push(a);
while(cin>>c>>b){
if(c=='*'){
a=x.top();
x.pop();
x.push(a*b%m);
}
else
x.push(b);
}
a=0;
while(x.size()){
a+=x.top();
a%=m;
x.pop();
}
cout<<a;
return 0;
}
``````
• @ 2016-10-19 15:21:24

#include<bits/stdc++.h>
using namespace std;
int b[1000000];
char a[1000000];
int main()
{
long i,j,k=0,n,m=0,x,y;
cin>>a;
n=strlen(a);
j=0;
for(i=0;i<n;i++){
m=0;
while(a[i]!='*'&&a[i]!='+'&&i<n){
m=m*10+a[i]-'0';
m%=10000;
i++;
}
b[j++]=m;
if(a[i]=='*') b[j++]=123123;
else if(a[i]=='+') b[j++]=0;
}
for(i=0;i<j;i++){
if(b[i]==123123){
b[i+1]*=b[i-1];
b[i+1]%=10000;
b[i]=b[i-1]=0;
}
}
for(i=0;i<j;i++){
k+=b[i];
k%=10000;
}
cout<<k<<endl;
return 0;
}

• @ 2016-10-19 15:00:09

#include<bits/stdc++.h>
using namespace std;
int b[1000000];
char a[1000000];
int main()
{
long i,j,k=0,n,m=0,x,y;
cin>>a;
n=strlen(a);
j=0;
for(i=0;i<n;i++){
while(a[i]!='*'&&a[i]!='+'&&i<n){
m*=10;
m+=a[i]-48;
m%=10000;
i++;
}
b[j++]=m;

if(a[i]=='*') b[j++]=123123;
else if(a[i]=='+') b[j++]=0;
m=0;
}
for(i=0;i<j;i++){
y=i+1;
if(b[i]==123123){
m=b[y-1]*b[y+1];
b[i-1]=m;
for(y=i;y<j;y++)
b[i]=b[y];
}
}
for(i=0;i<j;i++){
if(b[i]==0) k++;
k=(k+b[i]) % 10000;
}
cout<<k<<endl;
return 0;
}

• @ 2020-04-14 23:41:39
``````/*
注意第一个测试点是只输入一个数没有运算符
*/
#include <iostream>             //[2013普及组-B]表达式求值
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;

int main()
{
queue<ll> q;
queue<char> sym;
ll ans = 0;

string str;
cin >> str;

int a = 0, b;
for (int i = 0; i <= str.length(); i++)
{
b = i;
if(i == str.length() || str[i] == '+' || str[i] == '*')
{
ll k = stoi(str.substr(a, b - a));
q.push(k);
a = i + 1;
if(i != str.length())
sym.push(str[i]);
}
}

if(q.size() == 1)
{
ans = q.front() % 10000;
q.pop();
}
else
{
ll x, y, z;
char m;
x = q.front(), q.pop();
m = sym.front(), sym.pop();
y = q.front(), q.pop();

while (!q.empty())
{
z = q.front(), q.pop();
z %= 10000;
if (sym.front() == '*' && m == '+')
{
y *= z;
sym.pop();
}
else
{
if (m == '+')
x += y;
else
x *= y;
y = z;
m = sym.front(), sym.pop();
}
x %= 10000;
y %= 10000;
}

if (m == '+')
ans = x + y;
else
ans = x * y;
ans %= 10000;
}

cout << ans << endl;

return 0;
}

``````
• @ 2016-11-10 22:22:20

还是发个正经点的
评测结果
编译成功

Free Pascal Compiler version 3.0.0 [2015/11/16] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling foo.pas
foo.pas(23,8) Warning: Variable "num" does not seem to be initialized
41 lines compiled, 0.0 sec, 27056 bytes code, 1284 bytes data
1 warning(s) issued
测试数据 #0: Accepted, time = 0 ms, mem = 1268 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1496 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1492 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1496 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 1496 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 1496 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 1496 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 1500 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 4716 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 4716 KiB, score = 10
Accepted, time = 15 ms, mem = 4716 KiB, score = 100
代码
```pascal
{\$inline on}
const b10:array[0..3]of longint=(1,10,100,1000);
var a:ansistring;
num:array[1..100000]of longint;
lp,p:boolean;
i,j,lj,len,ans:longint;

function max(a,b:longint):longint;inline;
begin if a>b then exit(a);exit(b);end;

begin
len:=0;j:=1;lj:=1;
//fillchar(num,sizeof(num),0);
lp:=false;
repeat
inc(len);
while (a[j]>='0')and(a[j]<='9') do inc(j);
p:=(a[j]='*');
dec(j);
for i:=j downto max(lj,j-3) do
inc(num[len],(ord(a[i])-ord('0'))*b10[j-i]);
j:=j+2;lj:=j;
if lp then begin
dec(len);
num[len]:=(num[len]*num[succ(len)])mod 10000;
num[succ(len)]:=0;
end;
lp:=p;
until j>length(a);
{for i:=pred(len) downto 1 do
num[i]:=(num[i]*num[succ(i)])mod 10000;
dec(len);
for j:=succ(i) to len do num[j]:=num[succ(j)];
end;}
ans:=0;//writeln(len);
//for i:=1 to len do writeln(num[i]);
for i:=1 to len do ans:=(ans+num[i])mod 10000;
writeln(ans);
end.
```

• @ 2015-11-02 21:39:22

水题水过
Program P1849;

Var i,len,num,ans:longint;
s:ansistring;bo:boolean;
a:array[1..100000]of longint;

Begin
len:=length(s);
i:=1;num:=0;bo:=false;
while i<=len do begin
inc(num);
while (s[i]>='0') and (s[i]<='9') do begin

a[num]:=(a[num]*10+ord(s[i])-ord('0'))mod 10000;
inc(i);
if i>len then break;
end;
if bo then begin
a[num-1]:=(a[num]*a[num-1])mod 10000;
a[num]:=0;
dec(num);
end;

if i>len then break;
bo:=false;
if s[i]='*' then bo:=true;
inc(i);
end;
ans:=0;
for i:=1 to num do ans:=(ans+a[i])mod 10000;
writeln(ans);
end.

• @ 2015-10-22 22:05:32

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

char s[10000005];

int main(int argc, const char *argv[]) {
scanf("%s", s);
int l = strlen(s);
int buf = 0;
s[l++] = '+';
bool f = false;
int ans = 0;
int buf1 = 0;
for (int i = 0; i < l; ++i) {
if (s[i] == '+') {
if (f) {
buf = buf1;
buf %= 10000;
buf1 = 0;
}
f = false;
ans += buf;
buf = 0;
ans %= 10000;
continue;
}
if (s[i] == '
') {
if (f) {
buf *= buf1;
buf %= 10000;
buf1 = 0;
}
f = true;
continue;
}
if (f) {
buf1 = buf1 * 10 + s[i] - '0';
buf1 %= 10000;
continue;
}
buf = buf * 10 + s[i] - '0';
buf %= 10000;
}
printf("%d\n", ans);
return 0;
}

• @ 2016-08-24 20:02:01

好！

• @ 2015-09-04 16:13:04

#include<cstdio>
using namespace std;

long long ans=0,tans=1,t=0;
const int Mod=10000;

int main() {
char c; int tc=0;
bool have_cheng=0;
while ((tc=getchar())!=EOF) {
c=char(tc);
if (c!='+'&&c!='*'&&c!='\n') {
t=t*10+(c-'0'); t%=Mod;
} else {
if (c=='+') {
if (have_cheng){
ans+=(t*tans); ans%=Mod;
} else {
ans+=t; ans%=Mod;
}
t=have_cheng=0; tans=1;
}
if (c=='*') {
tans*=t; t=0; have_cheng=1; tans%=Mod;
}
}
}
ans+=(have_cheng?(tans*t):t); ans%=Mod;
printf("%lld",ans);
return 0;
}

• @ 2015-08-11 17:06:27

program expr;
label 10,20;
var
a:ansistring;
b,c,d:string;
l,m,n,x,y,s,k:int64;
i,j,code:longint;
begin
10:
l:=length(a);
i:=1;
while i<=l do
begin
if a[i]='*' then
begin
m:=i-1;
n:=i+1;
while (a[m]<>'+')and(a[m]<>'*') do
begin
m:=m-1;
if m=0 then break;
end;
m:=m+1;
while (a[n]<>'+')and(a[n]<>'*') do
begin
n:=n+1;
if n=l+1 then break;
end;
n:=n-1;
c:=copy(a,m,i-m);
d:=copy(a,i+1,n-i);
val(c,x,code);
val(d,y,code);
s:=x*y;
s:=s mod 10000;
str(s,b);
delete(a,m,n-m+1);
insert(b,a,m);
l:=length(a);
end;
i:=i+1;
end;
for i:=1 to l do
if a[i]='*' then goto 10;
20:
j:=1;
while j<=l do
begin
if a[j]='+' then
begin
m:=j-1;
n:=j+1;
while (a[m]<>'+')and(a[m]<>'*') do
begin
m:=m-1;
if m=0 then break;
end;
m:=m+1;
while (a[n]<>'+')and(a[n]<>'*') do
begin
n:=n+1;
if n=l+1 then break;
end;
n:=n-1;
c:=copy(a,m,j-m);
d:=copy(a,j+1,n-j);
val(c,x,code);
val(d,y,code);
s:=x+y;
s:=s mod 10000;
str(s,b);
delete(a,m,n-m+1);
insert(b,a,m);
l:=length(a);
end;
j:=j+1;
end;
l:=length(a);
for i:=1 to l do
if a[i]='+' then goto 20;
val(a,k,code);
k:=k mod 10000;
write(k);
end.

测试数据 #0: Accepted, time = 15 ms, mem = 796 KiB, score = 10
测试数据 #1: Accepted, time = 15 ms, mem = 1204 KiB, score = 10
测试数据 #2: Accepted, time = 15 ms, mem = 1204 KiB, score = 10
测试数据 #3: Accepted, time = 2 ms, mem = 1204 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
测试数据 #5: Accepted, time = 2 ms, mem = 1200 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 1200 KiB, score = 10
测试数据 #8: TimeLimitExceeded, time = 1015 ms, mem = 3188 KiB, score = 0
测试数据 #9: TimeLimitExceeded, time = 1015 ms, mem = 4184 KiB, score = 0
TimeLimitExceeded, time = 2079 ms, mem = 4184 KiB, score = 80

• @ 2015-08-07 21:52:21

var
ch:char;
a:array[1..10000]of longint;
i,s,l,ls:longint;
begin
l:=1;
while not eoln do
begin
case ch of
'0'..'9':begin
a[l]:=a[l]*10+ord(ch)-48;
end;
'+':begin
l:=l+1;
end;
'*':begin
while (ch>'0')and(ch<'9') do
begin
ls:=ls*10+ord(ch)-48;
end;
a[l]:=a[l]*ls;
end;
end;
end;
a[l]:=a[l]*10+ord(ch)-48;
s:=0;
for i:=1 to l do
s:=s+a[i];
writeln(s);
end.

• @ 2015-06-29 09:35:00

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;

char str1[1000002];
int len;
stack<char> fh;
stack<int> num;

int main(){
scanf("%s",str1);
len=strlen(str1);
int i=0;
char ch;
while(i<len){
if (str1[i]>='0' && str1[i]<='9'){
int x=0;
while(i<len && str1[i]>='0' && str1[i]<='9'){
x=x*10+str1[i]-'0';i++;
}
x%=10000;
num.push(x);
if(!fh.empty() && (fh.top()=='*' || (i<len && str1[i]=='+' ))){
int b,c;
b=num.top();num.pop();
c=num.top();num.pop();
ch=fh.top();fh.pop();
if (ch=='+') x=b+c;
if (ch=='*') x=(b%10000)*(c%10000);
x%=10000;
num.push(x);
}
if(i<len){
fh.push(str1[i]);i++;
}
}
}
while(!fh.empty()){
int x=0,b,c;
b=num.top();num.pop();
c=num.top();num.pop();
ch=fh.top();fh.pop();
if (ch=='+') x=b+c;
if (ch=='*') x=b*c;
x%=10000;
num.push(x);
}
cout<<num.top()<<endl;
}

• @ 2015-06-29 09:15:10

#include<cstdio>
#include<cstring>
#include<stack>
#include<iostream>
using namespace std;
int main()
{
stack<int> a;
stack<char> b;
char s[1000000];
char ch;
int l;
scanf("%s",&s);
int i=0;
l=strlen(s);
while (i<l){
if (s[i]>='0'&&s[i]<='9'){
int x=0;
while (i<l&&s[i]>='0'&&s[i]<='9'){
x=x*10+s[i]-'0';
i++;
}
x%=10000;
a.push(x);
if (!b.empty()&&(b.top()=='*'||(i<l&&s[i]=='+'))){
int m,n;
m=a.top();a.pop();
n=a.top();a.pop();
ch=b.top();b.pop();
if (ch=='+') x=(m+n)%10000;
if (ch=='*') x=(m*n)%10000;
a.push(x);
}
}
if (i<l){
b.push(s[i]);
i++;
}
}
while (!b.empty()){
int x,m,n;
ch=b.top();b.pop();
m=a.top();a.pop();
n=a.top();a.pop();
if (ch=='+') x=(m+n)%10000;
if (ch=='*') x=(m*n)%10000;
a.push(x);
}
printf("%d",a.top());
return 0;
}

• @ 2015-05-18 12:56:35

把原来的表达式用'+'分开，对于每一个部分，计算乘法。

### Pascal Code

var
s:ansistring;
i,len:longint;
last,ans:longint;
function xval(l,r:longint):longint; //返回s[l..r]的值，前提是s[l..r]处只有数字
var t,code:longint;
begin
val(copy(s,l,(r-l+1)),t,code);
exit(t mod 10000);
end;
function eval(l,r:longint):longint; //返回s[l..r]的值，前提是s[l..r]处只有数字和乘号
var i,t,last:longint;
begin
last:=l-1;
t:=1;
for i:=l to r do
begin
if s[i]='*' //用'*'分开
then begin
t:=((t mod 10000)*xval(last+1,i-1)) mod 10000;
last:=i; //更新上一个'*'的位置
end;
if i=r then t:=((t mod 10000)*xval(last+1,r)) mod 10000; //处理遇到右边界的情况
end;
exit(t mod 10000);
end;
begin
len:=length(s);
last:=0;
ans:=0;
for i:=1 to len do
begin
if s[i]='+' //用'+'分开
then begin
ans:=(ans+eval(last+1,i-1)) mod 10000;
last:=i; //更新上一个'+'的位置
end;
if i=len then ans:=(ans+eval(last+1,len)) mod 10000; //处理字符串结束的情况
end;
writeln(ans mod 10000);
end.

• @ 2015-05-12 21:45:28

#include <cstdio>
#include <stack>

using namespace std;

int main(int argc, char *argv[]){
stack<long long> ss;
stack<char> ops;
long long s;
int x;
char op;
scanf("%d", &x);
ss.push(x);

while(scanf("%c%d", &op, &x) == 2){
if(op == '*'){
s = ss.top();
ss.pop();
s *= x;
s %= 10000;
ss.push(s);
}else{
if(ss.size() == 2){
s = ss.top();
ss.pop();
s += ss.top();
ss.pop();
s %= 10000;
ss.push(s);
ops.pop();
}
ss.push(x);
ops.push('+');
}
}
if(ss.size() == 2){
s = ss.top();
ss.pop();
s += ss.top();
ss.pop();
s %= 10000;
ss.push(s);
}
printf("%d", (int)(ss.top() % 10000));
return 0;
}

• @ 2015-05-12 14:29:03

#include<cstdio>
#include<iostream>
using namespace std;
long long a[1000001];
char c[1000001];
int main() {
int i = 2;
cin >> a[1];
int js = 0;
while(scanf("%c", &c[i++]) != EOF){
scanf("%d", &a[i++]);
}
i-=2;
for(int j = i; j > 0; --j) {
if(c[j]=='*') {
a[j - 1] *= a[j + 1];
a[j - 1] %= 10000;
}
}
for(int j=1; j<=i; ++j) {
if(c[j]=='+') {
js += a[j + 1];
js %= 10000;
}
}
cout<< (js + a[1]) % 10000;
return 0;
}

• @ 2014-12-20 09:12:31

不解释。。。自行拷。。。。。
#include<iostream>
using namespace std;
int Compare(char a,char b){
if(a=='('&&b==')')return 0;
if(a=='('||b=='(') return -1;
if(b==')'||a=='/'||a=='*'||b=='+'||b=='-')return 1;
return -1;
}
int Cal(int a,char ch,int b){
if(ch=='+')return (a+b)%10000;
if(ch=='-')return (a-b)%10000;
if(ch=='*')return (a*b)%10000;
if(ch=='/')return (a/b)%10000;
}
int main(){
long long t,a[250]={0},k,bj;
char b[100006];
string s;
cin>>s;
b[++b[0]]='(';
s+=')';
k=0;
while(k<s.length()){
if(s[k]>='0'&&s[k]<='9'){
t=0;
while(s[k]>='0'&&s[k]<='9'){t=t*10+s[k]-'0';k++;}
a[++a[0]]=t;
}
bj=Compare(b[b[0]],s[k]);
if(bj==1){
a[a[0]-1]=Cal(a[a[0]-1],b[b[0]],a[a[0]]);
a[0]--;
b[0]--;
}
if(bj==0){b[0]--;k++;}
if(bj==-1){b[++b[0]]=s[k];k++;}
}
cout<<(a[a[0]]%10000)<<endl;
return 0;
}

• @ 2014-11-06 10:52:39

var
temp:int64;
i,top_num,top_sign,ans:longint;
heap_num:array[0..100000] of longint;
heap_sign:array[0..100000] of char;
s:ansistring;
begin
s:=s+'+0';
temp:=0;
for i:=1 to length(s) do
begin
if s[i] in ['0'..'9']then
begin
temp:=(temp*10+ord(s[i])-48) mod 10000;
end else
if s[i]='+' then
begin
if top_sign>0 then
begin
if heap_sign[top_sign]='*' then
begin
heap_sign[top_sign]:='+';
heap_num[top_num]:=(heap_num[top_num]*temp) mod 10000;
end else
if heap_sign[top_sign]='+' then
begin
heap_num[top_num]:=(heap_num[top_num]+temp) mod 10000;
end;
end else
if top_sign=0 then
begin
inc(top_sign);
heap_sign[top_sign]:='+';
inc(top_num);
heap_num[top_num]:=temp;
end;
temp:=0;

end else
if s[i]='*' then
begin
if top_sign>0 then
begin
if heap_sign[top_sign]='*' then
begin
heap_num[top_num]:=(heap_num[top_num]*temp) mod 10000;
end else
if heap_sign[top_sign]='+' then
begin
inc(top_sign);
heap_sign[top_sign]:='*';
inc(top_num);
heap_num[top_num]:=temp;
end;
end else
if top_sign=0 then
begin
inc(top_sign);
heap_sign[top_sign]:='*';
inc(top_num);
heap_num[top_num]:=temp;
end;
temp:=0;
end;
end;
ans:=0;
for i:=1 to top_num do
ans:=(ans+heap_num[i]) mod 10000;
writeln(ans);
end.
关于NOIP考前热身

• @ 2014-11-01 23:00:18

#include<stdio.h>
char expr[1000000000];
int p;

int factor();
int term();
int expression();

int factor()
{
int ans=0;
while (expr[p]<='9'&&expr[p]>='0')
ans=10*ans+expr[p++]-'0';

return ans;
}

int term()
{
int f1,f2;
f1=factor();
while(expr[p]=='*'||expr[p]=='/')
{
if(expr[p]=='*')
{
p++;
f2=factor();
f1%=10000;
f2%=10000;
f1*=f2;

}
if(expr[p]=='/')
{
p++;
f2=factor();
f1%=10000;
f2%=10000;
f1/=f2;

}
}
return f1;
}
int expression()
{
int f1,f2;
f1=term();
while(expr[p]=='+'||expr[p]=='-')
{

if(expr[p]=='+')
{
p++;
f2=term();
f1%=10000;
f2%=10000;
f1+=f2;

}
else
{
p++;
f2=term();
f1%=10000;
f2%=10000;
f1-=f2;

}
}
return f1%10000;
}
int main()
{

scanf("%s",expr);
p=0;
printf("%d\n",expression());
return 0;
}

ID
1849

7

(无)

3574

763

21%

10