0分求调

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

yhh20200707 @ 2024-12-15 12:16:23

#include<bits/stdc++.h>
using namespace std;
const int N=110;
char a[N];
char trans(int n)
{
    char a;
    if(n<10)
    {
        a=n+'0';
    }
    else
    {
        a=n-10+'A';
    }   
}
int main()
{
    int n,t,cnt=0;
    scanf("%d%d",&n,&t);
    while(n)
    {
        a[cnt]=trans(n%t);
        n=n/t;
        cnt++;  
    }
    for(int i=cnt-1;i>=0;i--)
    {
        cout<<a[i];
    }
    return 0;
}

by zhs_TLE @ 2024-12-15 12:51:27

trans中a建议换个变量名,trans没写返回值

#include<bits/stdc++.h>
using namespace std;
const int N=110;
char a[N];
char trans(int n)
{
    char s;
    if(n<10)
    {
        s=n+'0';
    }
    else
    {
        s=n-10+'A';
    }   
    return s;
}
int main()
{
    int n,t,cnt=0;
    scanf("%d%d",&n,&t);
    while(n)
    {
        a[cnt]=trans(n%t);
        n=n/t;
        cnt++;  
    }
    for(int i=cnt-1;i>=0;i--)
    {
        cout<<a[i];
    }
    return 0;
}

by zhs_TLE @ 2024-12-15 12:55:32

@yhh20200707


by Ybll_ @ 2024-12-15 13:01:24

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,x;
    string ans="";
    cin>>n>>x;
    while(n)
    {
        int sum=n%x;
        if(sum>9)ans=char('A'+sum-10)+ans;
        else ans=char('0'+sum)+ans;
        n/=x;
    }
    cout<<ans;
    return 0;
}

求关


by yhh20200707 @ 2024-12-20 21:57:58

感谢!感谢!


|