解题思路:模拟就完了,注意几个点,每次输入数的时候记得更改一下进制,已经输出的时候也要转化一下最近的进制
#include <bits/stdc++.h>
#define x first
#define y second
#define mem(h) memset(h,-1,sizeof h)
#define mcp(a,b) memcpy(a,b,sizeof b)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair< int , int > PII;
typedef pair< double , double > PDD;
namespace IO{
inline LL read ( ) {
LL o= 0 , f= 1 ; char c= getchar ( ) ;
while ( c< '0' || c> '9' ) { if ( c== '-' ) f= - 1 ; c= getchar ( ) ; }
while ( c>= '0' && c<= '9' ) { o= o* 10 + c- '0' ; c= getchar ( ) ; }
return o* f;
}
} using namespace IO;
const int N= 1e2 + 7 , M= 2e5 + 7 , INF= 0x3f3f3f3f , mod= 1e8 + 7 , P= 131 ;
char str[ 37 ] ;
stack< int > o;
stack< string> oo;
string op;
LL s, x;
char num[ N] , ans[ N] ;
int n;
void init ( ) {
for ( int i= 0 ; i< 10 ; i++ ) str[ i] = i+ '0' ;
for ( int i= 10 ; i<= 36 ; i++ ) str[ i] = ( i- 10 ) + 'A' ;
}
void change ( LL x, int jz) {
int cnt= 0 ;
while ( x) {
ans[ cnt++ ] = str[ x% jz] ;
x/ = jz;
}
for ( int i= cnt- 1 ; i>= 0 ; i-- ) {
cout<< ans[ i] ;
}
cout<< endl;
}
LL cal ( char * num, int jz) {
LL res= 0 ;
int len= strlen ( num) ;
for ( int i= 0 ; i< len; i++ ) {
if ( num[ i] >= '0' && num[ i] <= '9' ) {
res+ = ( num[ i] - '0' ) * pow ( jz, len- i- 1 ) ;
} else {
res+ = ( num[ i] - 'A' + 10 ) * pow ( jz, len- i- 1 ) ;
}
}
return res;
}
int main ( ) {
init ( ) ;
cin>> n;
o. push ( 10 ) ;
for ( int i= 0 ; i< n; i++ ) {
cin>> op;
if ( op== "CLEAR" ) {
s= 0 ;
} else if ( op== "NUM" ) {
cin>> num;
x= cal ( num, o. top ( ) ) ;
if ( ! oo. empty ( ) ) {
if ( oo. top ( ) == "ADD" ) {
s+ = x;
} else if ( oo. top ( ) == "SUB" ) {
s- = x;
} else if ( oo. top ( ) == "MUL" ) {
s* = x;
} else if ( oo. top ( ) == "DIV" ) {
s/ = x;
} else {
s% = x;
}
oo. pop ( ) ;
} else {
s= cal ( num, o. top ( ) ) ;
}
} else if ( op== "CHANGE" ) {
int k;
cin>> k;
o. pop ( ) ;
o. push ( k) ;
} else if ( op== "EQUAL" ) {
change ( s, o. top ( ) ) ;
} else {
oo. push ( op) ;
}
}
return 0 ;
}