7 CLEAR NUM 1024 CHANGE 2 ADD NUM 100000 CHANGE 8 EQUAL
2040
思路:把其他进制先转成10进制,最后在转成当前的进制输出
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> #include<vector> #include<cmath> const int maxn=1e5+5; typedef long long ll; using namespace std; string st; char str[36]; ll ss; void tochange(int x,int jz) { char ans[105]; int cnt=0; while(x) { ans[cnt++]=str[x%jz]; x/=jz; } for(int t=cnt-1;t>=0;t--) { cout<<ans[t]; } cout<<endl; } ll cal(char x[],int jz) { ll sum=0; int cnt=0; int len=strlen(x); for(int t=0;t<len;t++) { if(x[t]>=‘0‘&&x[t]<=‘9‘) { sum+=(x[t]-‘0‘)*pow(jz,len-1-t); } else { sum+=(x[t]-‘A‘+10)*pow(jz,len-1-t); } } return sum; } stack<int>op; stack<string>oop; int main() { op.push(10); for(int t=0;t<10;t++) { str[t]=‘0‘+t; } for(int t=10;t<36;t++) { str[t]=‘A‘+t-10; } int n; cin>>n; char num[105]; int cc; ll xx; for(int t=0;t<n;t++) { cin>>st; if(st=="CLEAR") { ss=0; } else if(st=="NUM") { cin>>num; xx=cal(num,op.top()); if(!oop.empty()) { if(oop.top()=="ADD") { ss+=xx; } else if(oop.top()=="SUB") { ss-=xx; } else if(oop.top()=="MUL") { ss*=xx; } else if(oop.top()=="DIV") { ss/=xx; } else { ss%=xx; } oop.pop(); } else { ss=cal(num,op.top()); } } else if(st=="CHANGE") { cin>>cc; op.pop(); op.push(cc); } else if(st=="EQUAL") { tochange(ss,op.top()); } else { oop.push(st); } } return 0; }
原文:https://www.cnblogs.com/Staceyacm/p/10808990.html