#include<iostream>usingnamespace std;intmain(){int t, d;//原数字,转换进制
cin >> t >>d;if(t ==0)//当原数字是 0时,最后结果还是 0{
cout <<0;return0;}int s[100];int i =0;while(t !=0){
s[i++]= t % d;
t = t / d;}for(int j = i -1; j >=0; j--){
cout << s[j];//一个个输出}return0;}
十进制转为 k 进制(k 为大于十)
#include<iostream>usingnamespace std;intmain(){int a, k, i =0;
cin >> a >> k;//原数字,转换进制char b[100];//是字符串数组if(a ==0){
cout <<0;return0;}while(a !=0){if(a % k >=10)
b[i++]= a % k +'A'-10;//记得要减 10else
b[i++]= a % k +'0';
a = a / k;}for(int k = i -1; k >=0; k--)
cout << b[k];return0;}
k 进制转为十进制(k 为大于十)
#include<iostream>#include<string>usingnamespace std;intmain(){
string s;int k;
cin >> s >> k;//原数字,转换进制int len = s.length();longlong sum =0;for(int i =0; i < len; i++){if(s[i]>='A'&& s[i]<='A'+ k -10)
sum = sum * k + s[i]-'A'+10;//注意加 10else
sum = sum * k + s[i]-'0';}
cout << sum;return0;}
d 进制转为十进制(d 为二至九)
#include<iostream>#include<string>usingnamespace std;intmain(){
string s;int d;
cin >> s >> d;//原数字,转换进制int len = s.length();longlong sum =0;for(int i =0; i < len; i++){
sum = sum * d + s[i]-'0';}
cout << sum;return0;}