405J1R训练课(T628724 10 进制转 x 进制)
pico_fence · · 个人记录
错误思路
看不懂题。
正确思路
先创建两个整数变量n和x,然后在创建一个字符串s变量,在输入n和x,再来个while循环,循环中要来个转换数字的:
while(n){
int t=n%x;
char c;
if(t>=10){
c=(t-10)+'A';
}else{
c=t+'0';
}
s=c+s;
n/=x;
}
最后输出s。
正确代码
#include<bits/stdc++.h>
using namespace std;
int n,x;
string s;
int main(){
cin>>n>>x;
while(n){
int t=n%x;
char c;
if(t>=10){
c=(t-10)+'A';
}else{
c=t+'0';
}
s=c+s;
n/=x;
}
cout<<s;
return 0;
}