1 条题解

  • 0
    @ 2025-4-12 22:06:18

    C++ :

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<queue>
    #include<iostream>
    #include<stack>
    #include<algorithm>
    #include<vector>
    #include<iterator>
    #include<map>
    #include<set>
    #include<cmath>
    #include<iostream>
    #include<string>
    using namespace std;
    string tr(int x,int y,string s)
    {
        string res="";
        int sum=0;
        for(int i=0;i<s.length();++i)
        {
            if(s[i]=='-')
                continue;
            if(s[i]>='0' && s[i]<='9')
                sum=sum*x+s[i]-'0';
            else
                sum=sum*x+s[i]-'a'+10;
        }
        while(sum)
        {
            char tmp=sum%y;
            sum/=y;
            if(tmp<=9)
                tmp+='0';
            else
                tmp=tmp-10+'a';
            res=tmp+res;
        }
        if(res.length()==0)
            res="0";
        if(s[0]=='-')
            res='-'+res;
        return res;
    }
    inline int mx(int a,int b)
    {
        return a>b?a:b;
    }
    inline int mi(int a,int b)
    {
        return a<b?a:b;
    }
    inline int abs1(int a)
    {
        return a>0?a:-a;
    }
    stack<char> s1;
    int main()
    {
        string an;
        int tot;
        int a,b;
        while(1)
        {
            while(!s1.empty())
                s1.pop();
            tot=0;
            string s;
            scanf("%d ",&a);
            cin>>s;
            if(a==0 && s=="0")
                break;
            if(a<0)
            {
                int flag=0,tot=0;
                if(s[0]=='-')
                {
                    flag=1;
                    tot++;
                }
                b=0;
                int w=tot;
                tot=1;
                for(int i=s.length()-1;i>=w;--i)
                {
                    b+=(s[i]-'0')*tot;
                    tot*=10;
                }
                if(flag==1)
                    b=-b;
                while(b)
                {
                    int k=b%a;
                    if(k<0)
                    {
                        k-=a;
                        b/=a;
                        b++;
                    }
                    else
                        b/=a;
                    if(k<10)
                        s1.push(k+'0');
                    else
                        s1.push((k-10)+'a');
                }
                while(!s1.empty())
                {
                int k=s1.top();
                 printf("%c",k);
                  s1.pop();
                 }
                  printf("\n");
     
            }
            else
            {
                an=tr(10,a,s);
                cout<<an<<endl;
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    2901
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者