zoukankan      html  css  js  c++  java
  • 2~36进制与2~36进制的高精度转换模板

    #include <bits/stdc++.h>
    #define inf 2333333333333333
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(long long i=a;i<=b;++i)
    
    using namespace std;
    long long n,m;
    string s;
    void in(long long &x){
        long long y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(long long x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    bool judge(string s){
        For(i,0,s.size()-1)
            if(s[i]!='0') return 1;
        return 0;
    }
    
    string solve(string s,long long n,long long m){
        string r,ans;
        long long d=0;
        if(!judge(s)) return "0";
        while(judge(s)){
            For(i,0,s.size()-1){       
                if(s[i]>='0' && s[i]<='9'){
                    if((d*n+s[i]-'0')/m>=10)
                        r+=(d*n+s[i]-'0')/m-10+'A';
                    else
                        r+=(d*n+s[i]-'0')/m+'0'; 
                    d=(d*n+(s[i]-'0'))%m;
                }
                else{
                    if((d*n+s[i]-'A'+10)/m>=10)
                        r+=(d*n+s[i]-'A'+10)/m-10+'A';
                    else
                        r+=(d*n+s[i]-'A'+10)/m+'0';
                    d=(d*n+(s[i]-'A'+10))%m;
                }      
            }
            s=r;
            r="";
            if(d>=10) ans+=d-10+'A';
            else ans+=d+'0';
            d=0;
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
    
    signed main(){
        while(cin>>s>>n>>m){
            cout<<solve(s,n,m)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    数组中的逆序对
    第一个只出现一次的字符
    丑数
    把数组排成最小的数
    整数中出现1的个数
    连续子数组最大和
    JS之window对象
    JS之递归(例题:猴子吃桃)
    JS中函数的基础知识
    JS数组2(冒泡排列、数组里面查找数据)
  • 原文地址:https://www.cnblogs.com/war1111/p/13215001.html
Copyright © 2011-2022 走看看