13:2进制转化为16进制

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int k=0;
	cin>>k;
	string res[1000];
	string s[1000];
	int num0=1;
	int num1=0;
		int allcounts=0;//记录截取每个字符串的位数 
		int sheng=0; 
	for(int i=0;i<k;i++)
	{
		
		cin>>res[i];
		s[i]="";//结果 存放字符串 
		int count=res[i].length();
		//
		//cout<<count<<endl;
		
		int yu=count%4;
		//
	//	cout<<yu;
		char a[10];
		num0=1;//8 4 2 1
		num1=0;//每4个处理的整数结果 
	  
		if(yu)
		{
			for(int j=0;j<yu;j++)
			{
			
				a[j]=res[i].at(j);
			    
			    //修正
				if((yu-j)==3)
			{
				num0=4;
			 } 
			else if((yu-j)==2)
			{
				num0=2;
			 } 
			 else if((yu-j)==1)
			{
				num0=1;
			 }  
			//cout<<a[j]<<endl;
			//
			//cout<<num0<<endl;
			
			//now 此处错误 
			//cout<<a[j]-'0'<<endl; 已修正 
				num1+=(a[j]-'0')*num0;
				//8 4 2 1此处错误 
			//cout<<num1<<endl;
			
			}
			//该出讲每4位处理结果变成字符串 
				if(num1>=10)
			{
				char c1='A'-10+num1;
				s[i]+=c1;
				
				//cout<<s[i]; 正确 
			
			 } 
			 else{
			 
			 char c0='0'+num1;
			 	s[i]+=c0;
			 	//right
			 	//cout<<s[i];
			 
			 }
		}
		
		sheng=count-yu;
		allcounts=yu;//weishu //此处很重要 
		int num3=0; 
		//cout<<sheng;
		
				
		while(sheng)
		{
			
			//num0=1;//2的倍数 
	    	num1=0;//余数的类型 
			for(int j=0;j<4;j++)
			{
				if((4-j)==4)
				{
					num0=8;
				}
				else if((4-j)==3)
				{
					num0=4;
				}
				else if((4-j)==2)
				{
					num0=2;
				}
			 else if((4-j)==1)
				{
					num0=1;
				}
				a[j]=res[i].at(allcounts++);
				num1+=(a[j]-'0')*num0;
				//num0*=2;
				
			}
		
			if(num1>=10)
			{
				char c0='A'-10+num1;
				s[i]+=c0; 
			 } 
			 else{
			 	 char c0='0'+num1;
			 	s[i]+=c0;
			 }
			
			sheng=sheng-4;
		}
		
	}
	//cout<<s[0];
	for(int i=0;i<k;i++)
	{
		if(i!=k-1)
		cout<<s[i]<<endl;
		else{
			cout<<s[i];
		}
	}

 } 

总时间限制: 

1000ms

 

内存限制: 

65536kB

描述

输入一个2进制的数,要求输出该2进制数的16进制表示。
在16进制的表示中,A-F表示10-15

输入

第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个以0和1组成的字符串,字符串长度至少是1,至多是10000

输出

n行,每行输出对应一个输入。

样例输入

2
100000
111

样例输出

20
7
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值