[藍橋杯]基礎練習 十進制轉十六進制

在這裏插入圖片描述
在這裏插入圖片描述

#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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章