c++如何将十进制数转换为d进制数,请各位天才帮帮忙
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
char c;
string s;
cin>>n>>t;
while(n!=0) {
if(t==16) {
if(n%16>=10) {
c=n%16-10+'A';
} else {
c=n%16+'0';
}
}else{
c=n%t+'0';
}
s=c+s;
n=n/t;
}
if(s=="") {
cout<<0;
} else {
cout<<s;
}
return 0;
}
上面代码哪里错了?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i=0,j,d,a[20]={0};
cin>>n>>d;
while(n!=0){
a[i]=n%d;
n=n/d;
i++;
}
for(j=i-1;j>=0;j--) cout<<a[j];
}