华为机试 - 火星计算器
来源:互联网 发布:恐惧未来知乎 编辑:程序博客网 时间:2024/06/10 04:08
火星计算器:
火星人使用运算符:@ # $ &
优先级为:@ > # > $ > &
X@Y = (X+1)*(Y+2)
X#Y = (2X+3)*(3Y+4)
X$Y = (3X+11)*(2Y+7)
X&Y = (5X+20)*(4Y+1)
X#Y@Z = X#(Y@Z)
编写火星计算器程序,计算火星文表达式;
例子:
输入:1@2
火星人使用运算符:@ # $ &
优先级为:@ > # > $ > &
X@Y = (X+1)*(Y+2)
X#Y = (2X+3)*(3Y+4)
X$Y = (3X+11)*(2Y+7)
X&Y = (5X+20)*(4Y+1)
X#Y@Z = X#(Y@Z)
编写火星计算器程序,计算火星文表达式;
例子:
输入:1@2
输出:8
#include <iostream>#include <string>#include <stack>using namespace std;int changeOp(char op){switch (op){case '&':return 1;case '$':return 2;case '#':return 3;case '@':return 4;}return 0;}int cal(int num1, int num2, int op){switch(op){case 1:return (5*num1+20)*(4*num2+1);case 2:return (3*num1+11)*(2*num2+7);case 3:return (2*num1+3)*(3*num2+4);case 4:return (num1+1)*(num2+2);}return 0;}int main(){string str;cin>>str;stack<int> numStack;stack<int> opStack;int num=0;char op;for (unsigned i=0;i<str.size();i++){if (str[i]>='0'&&str[i]<='9')num=num*10+str[i]-'0';else{op=str[i];numStack.push(num);num=0;if (numStack.size()==1){opStack.push(changeOp(op));continue;}int curOp=changeOp(op);int prevOp=opStack.top();if(prevOp>=curOp){int num1=numStack.top();numStack.pop();int num2=numStack.top();numStack.pop();numStack.push(cal(num1,num2,prevOp));opStack.pop();opStack.push(curOp);}else{opStack.push(curOp);}}}numStack.push(num);while (!opStack.empty()){int num1=numStack.top();numStack.pop();int num2=numStack.top();numStack.pop();numStack.push(cal(num2,num1,opStack.top()));opStack.pop();}cout<<numStack.top()<<endl;system("pause");return 0;}
0 0
- 华为机试 - 火星计算器
- 2015华为实习生-机试题-火星计算器
- 华为机试题:火星计算器 java版本
- 火星计算器
- 火星计算器
- 火星计算器
- OJ-火星???????计算器
- 2015上机笔试题-火星计算器
- 火星
- 华为机试
- 华为机试
- 华为机试
- 华为机试
- 华为机试4
- 华为机试
- 华为机试
- 华为机试-2014
- 华为机试
- OJ--分配糖果
- 开源ETL工具 Kettle
- 在Ubuntu14.04下使用ap-hotspot建立无线热点(AP mode)
- George and Cards - CodeForces 387E 树状数组
- 链栈
- 华为机试 - 火星计算器
- Java中写代码的快捷方式
- 反射机制实现EL表达式的解析过程
- 我的编程之路(二十一) 规范
- fork与vfork的区别
- 《硅谷传奇》短评
- 基于C#.net技术的数据安全删除系统的研究与实现
- Attribute is missing the Android namespace prefix问题解决
- NYOJ 2 括号配对问题