PAT甲级 1027 Colors in Mars 10进制转化为13进制
Solution:
这道题的意思是,将三个0~168的十进制数转化为13进制数(0 ~9,A ~C)并输出,若不够两位,则需要在左边补0。注意点:0 0 0 输出应为:#000000。
代码如下:
//10进制转化为13进制
#include<iostream>
#include<stdio.h>
using namespace std;
int a,b,c;//三个十进制数
int cnt1=0,cnt2=0,cnt3=0;//记录转化为13进制后的位数
char c1[2],c2[2],c3[2];//转化成的13进制数
int main(){
cin>>a>>b>>c;
int r;//temp为商,r为余数
int k=0;
while(a!=0){
r=a%13;
if(r==10){
c1[k++]='A';
}else if(r==11){
c1[k++]='B';
}else if(r==12){
c1[k++]='C';
}else{
c1[k++]=r+'0';
}
a/=13;
cnt1++;
}
k=0;
while(b!=0){
r=b%13;
if(r==10){
c2[k++]='A';
}else if(r==11){
c2[k++]='B';
}else if(r==12){
c2[k++]='C';
}else{
c2[k++]=r+'0';
}
b/=13;
cnt2++;
}
k=0;
while(c!=0){
r=c%13;
if(r==10){
c3[k++]='A';
}else if(r==11){
c3[k++]='B';
}else if(r==12){
c3[k++]='C';
}else{
c3[k++]=r+'0';
}
c/=13;
cnt3++;
}
cout<<'#';
if(cnt1==0){
cout<<"00";
}
else if(cnt1==1){
cout<<'0'<<c1[0];
}else{
cout<<c1[1]<<c1[0];
}
if(cnt2==0){
cout<<"00";
}
else if(cnt2==1){
cout<<'0'<<c2[0];
}else{
cout<<c2[1]<<c2[0];
}
if(cnt3==0){
cout<<"00";
}
else if(cnt3==1){
cout<<'0'<<c3[0];
}else{
cout<<c3[1]<<c3[0];
}
return 0;
}