0%

NOIP2013普及组表达式求值

P1981

这道题其实本身很简单。。
以 ‘+’ 分段,然后对于每一个段(只有乘法)做,然后计算时不要忘了时刻 % 10000
我就是加上最后一个数没有 % 10000。。40分

40pts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int P = 10000;
string s;

int calc(int l, int r)
{
if(l > r) return 0;
stringstream ss(s.substr(l, r - l + 1));
int base;
ss >> base;
char c; int xx;
while(ss >> c >> xx)
{
xx = xx % P;
if(c == '*') base = base * xx % P;
}
// printf("calc(%d, %d) = %d\n", l, r, base);
return base;
}

signed main(void)
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// cin >> s;
getline(cin, s);
int lastpos = 0, ans = 0;
for(int i = 0; i < s.length(); i ++)
{
if(s[i] == '+') ans += calc(lastpos, i - 1), ans = ans % P, lastpos = i + 1;
}
ans = ans + calc(lastpos, s.length() - 1);
printf("%d", ans);
}

100:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int P = 10000;
string s;

int calc(int l, int r)
{
if(l > r) return 0;
stringstream ss(s.substr(l, r - l + 1));
int base;
ss >> base;
char c; int xx;
while(ss >> c >> xx)
{
xx = xx % P;
if(c == '*') base = base * xx % P;
}
// printf("calc(%d, %d) = %d\n", l, r, base);
return base;
}

signed main(void)
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// cin >> s;
getline(cin, s);
int lastpos = 0, ans = 0;
for(int i = 0; i < s.length(); i ++)
{
if(s[i] == '+') ans += calc(lastpos, i - 1), ans = ans % P, lastpos = i + 1;
}
ans = (ans + calc(lastpos, s.length() - 1)) % P;
printf("%d", ans % P);
}

欢迎关注我的其它发布渠道