1 条题解

  • 2
    @ 2024-4-9 12:14:00

    本题只需要先将xx进制数转换成1010进制数,然后再从十进制数转换成yy进制数就可以了。
    注意C/C++提交需要高精度
    cpp代码

    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    #define sd(n) scanf("%d",&n)
    #define sdd(n,m) scanf("%d%d",&n,&m)
    #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define pd(n) printf("%d\n", n)
    #define pc(n) printf("%c", n)
    #define pdd(n,m) printf("%d %d", n, m)
    #define pld(n) printf("%lld\n", n)
    #define pldd(n,m) printf("%lld %lld\n", n, m)
    #define sld(n) scanf("%lld",&n)
    #define sldd(n,m) scanf("%lld%lld",&n,&m)
    #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
    #define sf(n) scanf("%lf",&n)
    #define sc(n) scanf("%c",&n)
    #define sff(n,m) scanf("%lf%lf",&n,&m)
    #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
    #define ss(str) scanf("%s",str)
    #define rep(i,a,n) for(int i=a;i<=n;i++)
    #define per(i,a,n) for(int i=n;i>=a;i--)
    #define mem(a,n) memset(a, n, sizeof(a))
    #define debug(x) cout << #x << ": " << x << endl
    #define pb push_back
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    #define mod(x) ((x)%MOD)
    #define gcd(a,b) __gcd(a,b)
    #define lowbit(x) (x&-x)
    typedef pair<int,int> PII;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long double ld;
    const int MOD = 1e9 + 7;
    const double eps = 1e-9;
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    const int inf = 0x3f3f3f3f;
    inline int read()
    {
        int ret = 0, sgn = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')
                sgn = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9')
        {
            ret = ret*10 + ch - '0';
            ch = getchar();
        }
        return ret*sgn;
    }
    inline void Out(int a)    //ê?3?ía1ò
    {
        if(a>9)
            Out(a/10);
        putchar(a%10+'0');
    }
    
    int qpow(int m, int k, int mod)
    {
        int res = 1, t = m;
        while (k)
        {
            if (k&1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    
    ll gcd(ll a,ll b)
    {
        return b==0?a : gcd(b,a%b);
    }
    
    ll lcm(ll a,ll b)
    {
        return a*b/gcd(a,b);
    }
    const int maxn = 1000;
    int  t[maxn], A[maxn];
    char str1[maxn], str2[maxn];
    int n, m;
    void solve()
    {
        int i, len, k;
        len = strlen(str1);
        for(i=len; i>=0; --i)
            t[len-1-i] = str1[i] -(str1[i]<58 ? 48: str1[i]<97 ? 55: 61);
        for(k=0; len;)
        {
            for(i=len; i>=1; --i)
            {
                t[i-1] +=t[i]%m*n;
                t[i] /= m;
            }
            A[k++] = t[0] % m;
            t[0] /=m;
            while(len>0&&!t[len-1])
                len--;
        }
        str2[k] =NULL;
        for(i=0; i<k; i++)
            str2[k-1-i] = A[i]+(A[i]<10 ? 48: A[i]<36 ? 55:61);
    }
    
    int main()
    {
        scanf("%d%d%s",&n, &m, str1);
        solve();
        printf("%s\n",str2);
        return 0;
    }
    

    Java代码

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String z;
    		int x,y;
    		Scanner input=new Scanner(System.in);
    		x=input.nextInt();
    		y=input.nextInt();
    		z = input.next();
    		if(z.equals("0"))
    		{
    			System.out.println(0);
    			return;
    		}
    		BigInteger big1=Toten(z, x);
    		String res=reverse(ToY(big1, y));
    		System.out.println(res);
    	}
    	
    	static String reverse(String str)
    	{
    		String s="";
    		int len = str.length();
    		for(int i = len-1;i >= 0;i--)
    		{
    			s+=str.charAt(i);
    		}
    		return s;
    	}
    	static String ToY(BigInteger a,int y) {
    		String res="";
    		BigInteger zero=new BigInteger("0");
    		BigInteger ynum=new BigInteger(""+y);
    		int tnum;
    		while(!a.equals(zero))
    		{
    			tnum=Integer.valueOf(a.remainder(ynum).toString());
    			res+=inttochar(tnum);
    			a=a.divide(ynum);
    		}
    		return res;
    	}
    	static BigInteger Toten(String str,int x) {
    		BigInteger res=new BigInteger("0");
    		BigInteger tb;
    		BigInteger tBigInteger=BigInteger.ONE;
    		BigInteger k = BigInteger.valueOf(x);
    		int len = str.length();
    		for(int i = len-1; i >= 0;i--){
    			tb=new BigInteger(tBigInteger.toString());
    			res=res.add(chartoint(str.charAt(i)).multiply(tb));
    			tBigInteger = tBigInteger.multiply(k);
    		}
    		return res;
    	}
    	static BigInteger chartoint(Character ch)
    	{
    		int t=0;
    		BigInteger bigInteger;
    	    if(ch>='0'&&ch<='9') {
    	    	t = ch-'0';
    	    }
    	    else if(ch>='A'&&ch<='Z') {
    	    	t = ch-'A'+10;
    	    }
    	    else if(ch>='a'&&ch<='z') {
    	    	t = ch-'a'+36;
    	    }
    	    bigInteger=new BigInteger(""+t);
    	    return bigInteger;
    	}
    	static Character inttochar(int n)
    	{
    	    if(n>=0&&n<=9)
    	        return (char) ('0'+n);
    	    else if(n>=10&&n<=35)
    	        return (char) ('A'+n-10);
    	    else
    	        return (char) ('a'+n-36);
    	}
    }
    
    • 1

    信息

    ID
    99
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    75
    已通过
    5
    上传者