首页 > 其他 > 详细

基础练习 16进制转8进制

时间:2020-01-21 13:41:36      阅读:62      评论:0      收藏:0      [点我收藏+]

  使用string 做容器不能通过样例,只能通过char数组来试试了

#include <iostream>
#include <string>

using namespace std;
int main()
{
    int n,i;
    string a,b,c;
    cin>>n;
    while(n--)
     {    
         int num=0;
        a="",b="",c="";
        cin>>a;
        for(i=0;i<a.length();i++)
        {
            switch(a[i])
            {
                case 0:b+="0000";break;
                case 1:b+="0001";break;
                case 2:b+="0010";break;
                case 3:b+="0011";break;
                case 4:b+="0100";break;
                case 5:b+="0101";break;
                case 6:b+="0110";break;
                case 7:b+="0111";break;
                case 8:b+="1000";break;
                case 9:b+="1001";break;
                case A:b+="1010";break;
                case B:b+="1011";break;
                case C:b+="1100";break;
                case D:b+="1101";break;
                case E:b+="1110";break;
                case F:b+="1111";break;
                default:break;
            }
        }
         if(b.length()%3==1){
            b.insert(0,"00");
        }else if(b.length()%3==2){
            b.insert(0,"0");
        }
    
        for(i=0;i<b.length();i+=3){
            num=4*(b[i]-0)+2*(b[i+1]-0)+(b[i+2]-0);
            c+=num+0; 
        }
        for(i=0;i<c.length();i++){
           while(c[i]==0) 
               i++;
            cout<<c[i];
        }
        cout<<endl;
    }
}

基础练习 16进制转8进制

原文:https://www.cnblogs.com/joker99/p/12221164.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!