[蓝桥杯]基础练习 十进制转十六进制

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<string>
#include<algorithm> 
using namespace std;

int main()
{
	int num = 0;
	cin>>num;
	if(num == 0)
	{
		cout<<"0";
		return 0; 
	}
	string res = "";
	while(num > 0)
	{
		int t = num%16;
		num /= 16;
		char ch;
		if(t >= 10)
		{
			ch = 'A' + t - 10;
		}
		else
		{
			ch = '0' + t;
		}
		res += ch;
	}
	reverse(res.begin(),res.end());
	cout<<res;
	return 0;
} 
发布了179 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40691051/article/details/104527614