0分求调,呜呜呜~

B3849 [GESP样题 三级] 进制转换

dailydream @ 2024-07-29 16:49:35

#include<bits/stdc++.h>

using namespace std;

int n, r;
char res[33];

int main()
{
    cin>>n>>r;
    int i = 0;
    while(n)
    {
        int d = n % r;
        if(d > 10)
            res[i] = d - 10 + 'A';
        else
            res[i] = d + '0';

        n /= r;

        i++;
    }

    for(int j = i; j >= 0; j--)
        cout<<res[j];

    return 0;
}

by lucasjj @ 2024-07-29 17:02:26

@dailydream

cin>>n>>x;
while(n!=0)
{
    int q=n%x;
    a[++top]=z(q);
    n/=x;
}
for(int i=top;i!=0;i--) cout<<a[i];
return 0;

}


by lucasjj @ 2024-07-29 17:03:37

哪个z()函数是用来转进制的


by lucasjj @ 2024-07-29 17:04:19

if(x<=9&&x>=0) return x+48; else return x+55;


by caojinyu0518 @ 2024-08-06 20:05:43

@dailydream

#include<bits/stdc++.h>
using namespace std;
int n,r,cnt;
char a[1000001];
int main(){
    cin>>n>>r;
    while(n!=0){
        cnt++;
        if(n%r>=10)a[cnt]='A'+n%r-10;
        else a[cnt]=n%r+'0';
        n/=r;
    }
    for(int i=cnt;i>=1;i--)cout<<a[i];
    return 0;
}

这样更直观一点


|