各种进制转换

今天看到雅虎一道面试题,竟然是一道进制转换的题,并且还不允许用printf()自带的函数库来做,所以就整理一下,以备不时之需;

十进制转换为其他进制:(十进制转换为其他进制可使用printf函数直接转换    库函数使用:http://blog.csdn.net/u014665013/article/details/40213033

(1)十进制转化为八进制

使用库函数

 int main()
{ 
 int n;
 scanf("%d",&n);
 printf("%O",n);
return 0;
}

自己代码:(递归方法)

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
 string str="";
string fun(int n)
{
	if(n==0)  //注意特殊情况,本题要求非负数 
	return "0";
	if(n>0)
	 {
	  int x=n%8;
	  fun(n/8);
	  str=str+char(x+'0');
	 } 
	 return str;
}
int main()
{ 
 int n;
 scanf("%d",&n);
cout<< fun(n);
return 0;
}

 

(2)十进制转十六进制

使用库函数

int main()
{ 
 int n;
 scanf("%d",&n);
 printf("%X",n);
return 0;
}

自己代码:

采用的递归方法

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
 string str="";
char exchenge(int n)
{
	if(n<10)
	  return n+'0';
	if(n==10)
	  return 'A';
	if(n==11)
	  return 'B';
	if(n==12)
	  return 'C';
	if(n==13)
	  return 'D';
	if(n==14)
	  return 'E';
	if(n==15)
	  return 'F';
}

string fun(int n)
{
	if(n==0)  //注意特殊情况,本题要求非负数 
	return "0";
	if(n>0)
	 {
	  int x=n%16;
	  fun(n/16);
	  str=str+exchenge(x);
	 } 
	 return str;
}

int main()
{ 
 int n;
 scanf("%d",&n);
cout<< fun(n);
return 0;
}


(二)其他进制转十进制

(1)八进制转十进制:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
  long long t;
  while (scanf("%I64o", &t) != EOF)   //note:use %I64
    printf("%I64d
", t);
    return 0;
}

(2)十六进制转为十进制(与上同)

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
  long long t;
  while (scanf("%I64x", &t) != EOF)   //note:use %I64
    printf("%I64d
", t);
    return 0;
}


自己代码:

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
 
int main()
{
char ch_16[10];
long long ch_10=0;
scanf("%s",ch_16);
int chlen=strlen(ch_16);
int i=0;
for(;i<chlen;i++)
 {
  if(ch_16[i]<='9'&&ch_16[i]>=0)
      ch_10=ch_10*16+ch_16[i]-'0';
  else 
  if(ch_16[i]>='A')
     ch_10=ch_10*16+ch_16[i]-'A'+10;
 }
 printf("%lld",ch_10);
return 0;
}

注:本代码不完全正确,有一组测试数据没有pass,为什么????? 
 


(三)其他进制转化为其他进制


(1)十六进制转化为八进制(数据比较小)(十六进制---->十进制---->八进制)

<span style="font-size:12px;">#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
 string str="";
string fun(int n)
{
	if(n==0)  //注意特殊情况,本题要求非负数 
	return "0";
	if(n>0)
	 {
	  int x=n%8;
	  fun(n/8);
	  str=str+char(x+'0');
	 } 
	 return str;
}
int _16To10(char ch){
    if(ch<='9'&&ch>='0')
 	 return int(ch-'0');
    if(ch>='A'&&ch<='F')
      return 10+ch-'A';
  
}
int main()
{ 
int Case;
scanf("%d",&Case);
while(Case--){
 char ch[100005];
 int n=0;
 cin>>ch;
 for(int i=0;i<strlen(ch);i++)
  n=n*16+_16To10(ch[i]);
cout<<fun(n)<<endl;
str=""; 
}
return 0;
}
</span>



数据较大时,需十六进制---->二进制------>八进制

代码:

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char str[200005];
 
int main()
{	
 int Case;
scanf("%d",&Case);
while(Case--){
string str_16;
string str_2="";

 cin>>str_16;

 //修正位 
 int str_16len=str_16.length();
 if(str_16len%3==1)
  str_2="00"+str_2;
 if(str_16len%3==2)  
   str_2="0"+str_2; 

  
  //16进制转化为2进制 
 	
     for( int i=0;i<str_16len;i++)
    {
       if(str_16[i]=='0')
          str_2+="0000";
       else if(str_16[i]=='1')
          str_2+="0001";
       else if(str_16[i]=='2')
          str_2+="0010";
       else if(str_16[i]=='3')
           str_2+="0011";
       else if(str_16[i]=='4')
          str_2+="0100";
       else if(str_16[i]=='5')
          str_2+="0101";
       else if(str_16[i]=='6')
          str_2+="0110";
       else if(str_16[i]=='7')
          str_2+="0111";   
       else if(str_16[i]=='8')
          str_2+="1000";
       else if(str_16[i]=='9')
          str_2+="1001";
       else if(str_16[i]=='A')
          str_2+="1010";
       else if(str_16[i]=='B')
          str_2+="1011";
       else if(str_16[i]=='C')
          str_2+="1100";
       else if(str_16[i]=='D')
          str_2+="1101";
       else if(str_16[i]=='E')
          str_2+="1110";
       else if(str_16[i]=='F')
          str_2+="1111";
    }


   int bit=str_2.length() ,j;
    int i;
	for(i=0,j=0;i<=bit-3;i=i+3)
	{
	 str[j]=(str_2[i]-'0')*4+(str_2[i+1]-'0')*2+str_2[i+2]  ;
	 j++;
	}
	
     i=0;
	if(!(str[i]=='0'))
      printf("%c",str[i]);
	for(i=1;i<j;++i)
      printf("%c",str[i]);
     printf("
");
}
return 0;
}
在做这道题的时候提交出过问题(+ 运算符),网址 http://blog.csdn.net/u014665013/article/details/43031619

(2)八进制转化为16进制(同上)





 





原文地址:https://www.cnblogs.com/zswbky/p/5432025.html