第1个回答 2021-10-18
可以用一个自定义的进制转换函数,用来处理二进制三进制的输出,其余的八进制,十进制和16进制可以使用C++语言自带的格式输出函数进行输出。
#include <iostream>
using namespace std;
void dec2k(int n,int k)
{ if(n>k-1)dec2k(n/k,k);
printf("%d",n%k);
}
int main()
{ int a,b,i;
scanf("%d%d",&a,&b);
printf("二进制\t三进制\t八进制\t十进制\t十六进制\n");
for(i=a; i<=b; i++)
{ dec2k(i,2);
printf("\t");
dec2k(i,3);
printf("\t%o\t%d\t%X\n",i,i,i);
}
return 0;
}本回答被网友采纳
第2个回答 2021-10-18
#include<iostream>
using namespace std;
void tobase(unsigned int num, unsigned int base)
{
if(num >= base)
tobase(num/base, base);
cout << "0123456789ABCDEF"[num%base];
}
int main()
{
unsigned int n1, n2;
cin >> n1 >> n2;
if(n2 < n1)
{
n1 = n1 ^ n2;
n2 = n1 ^ n2;
n1 = n1 ^ n2;
}
for(; n1 <= n2; n1++)
{
tobase(n1, 2);
cout << "\t";
tobase(n1, 3);
cout << "\t";
tobase(n1, 8);
cout << "\t";
tobase(n1, 10);
cout << "\t";
tobase(n1, 16);
cout << endl;
}
return 0;
}