十进制转十六进制

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    int a,r;
    cin>>a;
    char c;
    vector<char>ch;
    vector<char>::reverse_iterator rit;
    if(a==0)
        ch.push_back(0+'0');
    while(a!=0)
    {
        r=a%16;
        switch(r)
        {
            case 10:c='A';break;
            case 11:c='B';break;
            case 12:c='C';break;
            case 13:c='D';break;
            case 14:c='E';break;
            case 15:c='F';break;
            default:
                c=r+'0';break;
        }
        ch.push_back(c);
        a=(a-r)/16;
    }
    for(rit=ch.rbegin();rit!=ch.rend();rit++)
        cout<<*rit;
    return 0;
}

 

posted @ 2021-07-16 21:08  z9m8r8  阅读(49)  评论(0编辑  收藏  举报