11 条题解
-
14
提示
题目关键在于如何处理2个不同的运算符。根据数学知识,我们知道先乘除再加减。如果我们先计算了所有的乘法之后,那么最后一步就是若干个乘积相加。所以我们可以使用栈暂存这些乘积,最后遍历栈求和就好了。需要注意题目只需要最后4位,可以定义m=10000,作取余操作得到最后答案。思路
首先特殊处理第一个数字,取余数之后,直接进栈。之后使用while循环读入,根据读入的运算符,分两种情况讨论:
情况一:‘*’,需要处理乘法操作,将栈顶元素取出与新数字作乘法,之后将栈顶元素重新赋值为这个乘积,也可以合并成一步:栈顶元素修改为栈顶元素与新数字的乘积。
情况二:‘+’,直接将新的数字进栈暂存。
处理好输入之后,遍历栈求和即可。再次提醒每一步计算时都需对m取余。代码
#include <iostream> using namespace std; int stk[100005], top = 0, m = 10000; int main() { int a, b; char c; cin >> a; a = a % m; top++; stk[top] = a; while (cin >> c >> b) { if (c == '*') { stk[top] = stk[top] * b % m; } else { top++; stk[top] = b; } } a = 0; while (top) { a += stk[top]; a %= m; top--; } cout << a << endl; return 0; }
-
13
P1033
-题目回顾-
懒得回顾了,想看自己看
-分析-
模拟栈,处理好输入之后,遍历栈求和即可。
-代码-
看我的超级压缩版!(共13行)
#include <bits/stdc++.h>//by AGOMG using namespace std; int stk[100005], top = 0, m = 10000; int main(){int a, b;char c;cin >> a;a = a % m;top++; stk[top] = a; while (cin >> c >> b){ if (c == '*')stk[top] = stk[top] * b % m; else{top++;stk[top] = b;} }a = 0; while (top){a += stk[top];a %= m;top--;} cout << a << endl; return 0; }
-
7
题目:[NOIP2013 普及组] 表达式求值 链接:http://oj.hetao101.com/d/camp/p/260或http://oj.hetao101.com/d/camp/p/260?tid=64d0b45c9805cb7f84df4a29 题解:
#include <iostream> using namespace std; int stk[100005],top = 0,m = 10000; int main() { int a,b; char c; cin >> a; a = a % m; top++; stk[top] = a; while (cin >> c >> b) { if (c == '*') stk[top] = stk[top] * b % m; else { top++; stk[top] = b; } } a = 0; while (top) { a += stk[top]; a %= m; top--; } cout << a << endl; return 0; }
-
6
嗯,嗯,嗯......
while (cin >> c >> b){ //输入(c是char,b是int) if (c == '*'){ s.top() = s.top() * b % 10000; //先乘后加 } else{ s.push(b); //加数入栈 } } while (s.size()){ //循环栈的长度次 sum += s.top(); //计算栈顶元素之和 sum %= 10000; //每次抹10000(自己想想为什么) s.pop(); //栈顶元素出栈 } //第一个入栈的得自己定义!!!(比如:int a; 变量名.push(a);)
核心代码!
我就是玩! -
4
看没人用STL库的容器就随手写了一个
#include <bits/stdc++.h> using namespace std; const int m=10000; int a,b,sum,pos; char c; stack<int> s; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> a; s.push(a); while(cin >> c >> b) { if(c=='*') { pos=s.top()%m; s.pop(); s.push(pos*b%m); } else { pos=b%m; s.push(b%m); } } int s_size=s.size(); while(s_size) { sum+=s.top(); sum%=m; s.pop(); s_size--; } cout << sum << "\n"; }
-
3
题目回顾
自己回去看
分析
模拟栈,处理好输入之后,遍历栈求和即可。代码
#include <iostream> using namespace std; int stk[100005],top = 0,m = 10000; int main() { int a,b; char c; cin >> a; a = a % m; top++; stk[top] = a; while (cin >> c >> b) { if (c == '*') stk[top] = stk[top] * b % m; else { top++; stk[top] = b; } } a = 0; while (top) { a += stk[top]; a %= m; top--; } cout << a << endl; return 0; }
-
-1
#include <bits/stdc++.h>//改编 AGOMG 的代码 int stk[100005], top = 0, m = 10000; int main(){int a, b;char c;scanf("%d", &a);a = a % m;top++;stk[top] = a; while (std::cin >> c >> b){ if (c == '*')stk[top] = stk[top] * b % m; else{top++;stk[top] = b;} }a = 0; while (top){a += stk[top];a %= m;top--;}printf("%d", a); }
改编(超级压缩,9行)
-
-2
我再压!
#include <bits/stdc++.h>//改编 AGOMG 的代码 int stk[100005], top = 0, d = 10000; int main(){int a, b;char c;scanf("%d", &a);a = a % d;top++;stk[top] = a; while (std::cin >> c >> b){if (c == '*')stk[top] = stk[top] * b % d; else{top++;stk[top] = b;}}a = 0; while (top){a += stk[top];a %= d;top--;}printf("%d", a);}
超级压缩:8行
-
-2
#include <bits/stdc++.h> using namespace std;int stk[100015],top=0,m=10000,a,b;char c;int main(){cin>>a;a=a%m;top++;stk[top]=a;while(cin>>c>>b){if(c=='*'){stk[top] = stk[top] * b % m;}else{top++;stk[top] = b;}}a=0;while(top){a += stk[top];a %= m;top--;}cout<<a<<'\n';return 0;}
(真真的压缩,改编AGOMG)
- 1
信息
- ID
- 24
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 1435
- 已通过
- 729
- 上传者