qiu

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

Wang_xin_chen @ 2024-09-28 16:55:39

#include<bits/stdc++.h>
using namespace std;
int n,r;
string s="";
int main(){
    cin>>n>>r;
    while(n>0){
        if(n%r>9){
            char c=n%r+55;
            s+=c;
        }
        else{
            int i=n%r;
            cout<<i;
        }
        //cout<<s<<endl;
        n/=r;
    }
    for(int i=(s.size()-1);i>=0;i--){
        //swap(s[i],s[i-s.length()]);
        cout<<s[i];
    }
    return 0;
}

by szlh_XJS @ 2024-09-28 16:59:36

@Wang_xin_chen string和char好像不能直接加,把这个改一下


by Wang_xin_chen @ 2024-09-28 17:00:35

@szlh_XJS A了!


by zh_felix @ 2024-09-28 17:01:51

它有可能字母在数字中间,所以应该把数字也加到字符串里:

#include<bits/stdc++.h>
using namespace std;
int n,r;
string s="";
int main(){
    cin>>n>>r;
    while(n>0){
        if(n%r>9){
            char c=n%r+55;
            s+=c;
        }
        else{
            char c=n%r+'0';
            s+=c;
        }
        //cout<<s<<endl;
        n/=r;
    }
    for(int i=(s.size()-1);i>=0;i--){
        //swap(s[i],s[i-s.length()]);
        cout<<s[i];
    }
    return 0;
}

AC记录


by xywuyu @ 2024-09-28 17:02:02

#include<bits/stdc++.h>
using namespace std;
string f(int n,int a)
{string y="";
while (n!=0)
{if (n%a>=10)
{y+=char(n%a-10+'A');
n/=a;continue;
}
y+=char(n%a+'0');
n/=a;
}
return y;
}
int main()
{
    int n,t;
    cin>>n>>t;
    for (int i=f(n,t).size()-1;i>=0;i--)
    {cout<<f(n,t)[i];
    }

    return 0;

 } 

by szlh_XJS @ 2024-09-28 17:03:37

@Wang_xin_chen

给个关


by zh_felix @ 2024-09-28 17:05:25

@szlh_XJS stringchar 可以加


by szlh_XJS @ 2024-09-28 17:10:03

@zh_felix 那他是怎么A的?


by zh_felix @ 2024-09-28 17:14:55

I don't know.


|