給出一個非負整數,將它表示成十六進制的形式。
/*十進制轉十六進制*/
#include<cstdio>
int main()
{
const int maxn=10;
int x,i=0;
scanf("%d",&x);
char str[maxn];
if(x==0)//輸入爲0的情況單列
printf("0");
while(x)//輸入不爲0時,將x對應的十六進制數倒序存入字符串str中
{
int t=x%16;
if(t<10)
str[i++]='0'+t;
else
str[i++]='A'+(t-10);
x/=16;
}
while(i)//倒序輸出str
{
printf("%c",str[--i]);
}
printf("\n");
return 0;
}