C++语言十进制转十六进制的代码

#include 
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000007
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair P;
const int maxn = 27;
ll n;
mapchar>mp;

void init() {
    mp[0]='0';mp[1]='1';mp[2]='2';mp[3]='3';mp[4]='4';mp[5]='5';
    mp[6]='6';mp[7]='7';mp[8]='8';mp[9]='9';mp[10]='A';mp[11]='B';
    mp[12]='C';mp[13]='D';mp[14]='E';mp[15]='F';
}

int main() {
    init();
    scanf("%lld",&n);
    if(n==0) {
        printf("0
");
    } else {
        stack ans;
        ll y=0;
        while(n) {
            y = n%16;
            n = n/16;
            ans.push(mp[y]);
        }
        while(!ans.empty()) {
            printf("%c",ans.top());
            ans.pop();
        }
        printf("
");
    }

    return 0;
}

 

你可能感兴趣的