Files
python/C++专题课程/进制转化/10进制转16进制.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

26 lines
445 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
int main() {
long a;//输入的数
int y=0;//循环中的余数
string s="";//输出的结果
cin>>a;
if(a==0) { //0比较特殊,单独处理
cout<<0;
return 0;
}
while(a>0) { //大于0的数
y=a%16; //求余
if(y<10) //小于10的余数
s=char('0'+y)+s; //利用字符的ascll码在字符串前面拼接
else
s=char('A'-10+y)+s; //大于9的余数用ABCDE表示
a=a/16;
}
cout<<s;
return 0;
}