• 蓝桥杯 PREV-55 小计算器


    思路:

    输入、输出数据的时候进行进制转化,过程中均用long long类型保存、运算,执行CLEAR时进制不能改变,其它照着题意模拟即可;

    代码:

    #include<iostream>
    using namespace std;
    typedef long long LL;
    LL x,y;
    LL base=10;
    bool fst;//当前是否输入基础值 
    string cmd;
    LL to_num(char c){
    	if(c>='0'&&c<='9') return c-'0';
    	else return c-'A'+10;
    }
    char to_c(LL n){
    	if(n>=0&&n<=9) return n+'0';
    	else return n-10+'A';
    }
    LL to_Dec(string s){
    	LL rs=0,pow=1;
    	for(int pos=s.length()-1;pos>=0;pos--){
    		rs+=to_num(s[pos])*pow;
    		pow*=base;
    	}
    	return rs;
    }
    void print(){
    	string s="";
    	LL num=x;
    	do{
    		s=to_c(num%base)+s;
    		num/=base;
    	}while(num);
    	cout<<s<<'
    ';
    }
    void cal(){
    	if(cmd=="ADD") x+=y;
    	else if(cmd=="SUB") x-=y;
    	else if(cmd=="MUL") x*=y;
    	else if(cmd=="DIV") x/=y;
    	else if(cmd=="MOD") x%=y;
    }
    int main(){
    	int n;
    	cin>>n;
    	while(n--){
    		string s;
    		cin>>s;
    		if(s=="NUM"){
    			string num;
    			cin>>num;
    			if(fst) x=to_Dec(num);
    			else{
    				y=to_Dec(num);
    				cal();
    			}
    		}else if(s=="ADD"||s=="SUB"||s=="MUL"||s=="DIV"||s=="MOD"){
    			cmd=s;
    			fst=0;
    		}
    		else if(s=="CHANGE") cin>>base;
    		else if(s=="EQUAL") print();
    		else if(s=="CLEAR") {x=y=0;fst=1;}
    	}
    	return 0;
    }
    
  • 相关阅读:
    springboot热部署
    maven换仓库地址
    floyd求最小环+例题(hdu1599)
    矩阵乘法+folyd(hdu2807)
    TSP问题+例题
    迪杰斯特拉模板题(迪杰斯特拉模板)
    小w的糖果
    DongDong坐飞机
    DongDong跳一跳
    主席树入门
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308855.html
Copyright © 2020-2023  润新知