基础训练 16进制转为8进制

16进制转为8进制

#include<iostream>
#include<string.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	for(int j=0; j<n; j++){
		string s, str="";
		cin>>s;
		for(int i=0; i<s.size(); i++){
			switch(s[i]){
				case '0': str+="0000"; break;
				case '1': str+="0001"; break;
				case '2': str+="0010"; break;
				case '3': str+="0011"; break;
				case '4': str+="0100"; break;
				case '5': str+="0101"; break;
				case '6': str+="0110"; break;
				case '7': str+="0111"; break;
				case '8': str+="1000"; break;
				case '9': str+="1001"; break;
				case 'A': str+="1010"; break;
				case 'B': str+="1011"; break;
				case 'C': str+="1100"; break;
				case 'D': str+="1101"; break;
				case 'E': str+="1110"; break;
				case 'F': str+="1111"; break;
		    	default: break;
			}
		}
		if(s.size()%3==2)
			str="0"+str;
		else if(s.size()%3==1)
			str="00"+str;
		int flag=0;
		for(int i=0; i<str.size(); i+=3){
			int t=(str[i]-'0')*4+(str[i+1]-'0')*2+(str[i+2]-'0');
			if(t!=0) flag=1;
			if(flag) cout<<t;
		}
		cout<<endl;
	}
	return 0;
} 
posted @ 2019-01-23 21:20  A-Little-Nut  阅读(206)  评论(0编辑  收藏  举报