蓝桥杯训练记录
作者:互联网
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define ull unsigned long long 4 #define mem(a,x) memset(a,x,sizeof(a)) 5 using namespace std; 6 7 const int MAXN=1e6+5; 8 9 int ope,cnt,a[MAXN]; 10 ll n,tmp,ans,k; 11 string s,tmps; 12 13 void change2ten() { 14 tmp=0; 15 for(int i=0; i<tmps.size(); ++i) { 16 tmp*=k; 17 if(tmps[i]<='9'&&tmps[i]>='0') { 18 tmp+=tmps[i]-'0'; 19 } else { 20 tmp+=tmps[i]-'A'+10; 21 } 22 } 23 } 24 25 void change2k() { 26 if(!ans) {cout<<0<<endl; return;} 27 cnt=0,tmp=ans; 28 while(tmp) { 29 a[++cnt]=tmp%k; 30 tmp/=k; 31 } 32 for(int i=cnt; i>=1; --i) 33 if(a[i]<=9) cout<<a[i]; 34 else printf("%c",a[i]+'A'-10); 35 cout<<endl; 36 } 37 38 int main() { 39 cin>>n; 40 k=10; 41 for(int i=1; i<=n; ++i) { 42 cin>>s; 43 if(s=="EQUAL") change2k(); 44 else if(s=="CLEAR") tmp=ans=ope=0; 45 else if(s=="ADD") 46 ope=1; 47 else if(s=="SUB") 48 ope=2; 49 else if(s=="MUL") 50 ope=3; 51 else if(s=="DIV") 52 ope=4; 53 else if(s=="MOD") 54 ope=5; 55 else if(s=="NUM") { 56 cin>>tmps; 57 change2ten(); 58 switch(ope) { 59 case 0: 60 ans=tmp; 61 break; 62 case 1: 63 ans+=tmp; 64 break; 65 case 2: 66 ans-=tmp; 67 break; 68 case 3: 69 ans*=tmp; 70 break; 71 case 4: 72 ans/=tmp; 73 break; 74 case 5: 75 ans%=tmp; 76 break; 77 } 78 } else if(s=="CHANGE") 79 cin>>k; 80 81 } 82 }小计算器
标签:tmp,break,训练,记录,else,蓝桥,ope,case,ans 来源: https://www.cnblogs.com/chunibyo/p/10354112.html