100分,求优化

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

fzc888888882023 @ 2024-10-11 22:27:59

#include <iostream>
using namespace std;

const long MAX = 1e5;
void calc(int, int);

int main()
 {
    int n, jz;
    cin >> n >> jz;
    calc(n, jz);
    return 0;
}

void calc(int n, int jz)
{
    char a[MAX], cnt;
    for (int i = 1; n; i++)
    {
        int num = n % jz;
        a[i] = num + 48;
        if (num >= 10)
        {
            a[i] = 64 + (num - 9);
        }
        n /= jz;
        cnt = i;
    }
    for (int i = cnt; i >= 1; i--)
    {
        cout << a[i];
    }
    cout << endl;
    return ;
}

by Lots_of @ 2024-10-11 22:34:54

@fzc888888882023 提供一个模板参考一下

#include <bits/stdc++.h>
using namespace std;
string Immorta_to_x(int num,int x){
    string sum;
    while(num!=0){
        if(num%x<10){
            sum=char(num%x+'0')+sum;
        } 
        else {
            sum=char(num%x-10+'A')+sum;
        }    
        num/=x;
    } 
    return sum; 
} 
int main(){
    long long n,m;//n代表十进制,m代表待转换的进制 
    cin>>n>>m;
    cout<<Immorta_to_x(n,m);
    return 0;
}

by M_C_Yyyds @ 2024-10-13 09:57:14

@fzc888888882023

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

仅供参考


by fzc888888882023 @ 2024-10-16 21:08:50

@FY_Immorta 谢谢


by fzc888888882023 @ 2024-10-16 21:09:32

@M_C_Yyyds 感谢


by lww123 @ 2024-10-18 18:33:47

楼主送你个好东西->#include <bits/stdc++.h>


|