蓝桥杯 十六进制转八进制

数据太大,用循环超时,看了别人的答案豁然开朗,自己重写了一遍。

#include<iostream>
#include<map>
#include<ctype.h>
#include<string>

using namespace std;

int main(int argc, char **argv) {
  string str_16[]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100",
 				   "1101","1110","1111"};

  map<string,string>map_8;
  map_8["000"]="0";
  map_8["001"]="1";
  map_8["010"]="2";
  map_8["011"]="3";
  map_8["100"]="4";
  map_8["101"]="5";
  map_8["110"]="6";
  map_8["111"]="7";

  
  int n;
  cin>>n;
  
  while(n--){
	string str;
	string str_2="";
	string str_10="";
	cin>>str;

	for(int i=0;i<str.length();i++)
	  {
		if(isalpha(str[i])){
		  str_2+=str_16[str[i]-'A'+10];
		}
		else if(isdigit(str[i])){
		  str_2+=str_16[str[i]-'0'];
		}
	  }

	if(str_2.length()%3==1)
	  str_2="00"+str_2;
	if(str_2.length()%3==2)
	  str_2="0"+str_2;

	
	string re="";
	for(int i=0;i<str_2.length();i+=3){
	  string t=str_2.substr(i,3);
	  re+=map_8[t];
	}
	string::iterator p=re.begin();


	while(*p++=='0');
	--p;
	while(p!=re.end()){
	  cout<<*p;
	  p++;
	}
	cout<<endl;
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值