十进制转为二进制
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1000;
int n, num;
int a[N];
int main(){
cin >> n;
do{ //不用while循环,可以有效处理n == 0的情况
a[num ++] = n % 2;
n /= 2;
}while(n != 0);
for(int i = num - 1; i >= 0; i --){
cout << a[i];
}
return 0;
}
十进制转为十六进制
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1000;
int n, num;
int a[N];
char h[17] = {'0', '1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//十六进制
int main(){
cin >> n;
do{ //不用while循环,可以有效处理n == 0的情况
a[num ++] = n % 16;
n /= 16;
}while(n != 0);
for(int i = num - 1; i >= 0; i --){
int n = a[i];
cout << h[n];
}
return 0;
}
orz