进制转化

16--->8

//2进制
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define N 100100
char a[N];
int cnt = 0,ans=0;
int b[N*4],c[N*4];
int l1,l;
void chan1(int x)
{
    for(int i =8;i>=1;i/=2)
    {
        b[cnt] =x/i;
        x-=b[cnt]*i;
        cnt++;
    }
}
void chan2(int b[])
{

    
    for(int j =0;j+2<=cnt;j+=3)
    {
        if(j==0&&b[0]+b[1]+b[2]==0) continue;
        int q = 4*b[j]+2*b[j+1]+b[j+2];
        c[ans++] = q;
        
    }
    
    
}


int main()
{
    
    scanf("%s",a);
    l =strlen(a);
    if(l==1&&a[0]=='0') 
    {
    printf("0\n");
    return 0;
}
    int x;
    for(int i =0;i<l;i++){
       if(isupper(a[i]))   x = a[i]-'A'+10;
       else x = a[i]-'0';
       chan1(x);
    }
    /*
    for(int i=0;i<cnt;i++) printf("%d",b[i]);
    printf("\n");
    */
    if(cnt%3!=0){
        int w=cnt%3;
        w=3-w;
        for(int i =cnt+w-1;i>=w;i--) b[i] =b[i-w];//右移w位 
        for(int i =0;i<w;i++) b[i] = 0;    //补零 
        cnt+=w;
    }
    
        /*for(int i=0;i<cnt;i++) printf("%d",b[i]);
        printf("\n");
        */
    chan2(b);
    for(int j =0;j<ans;j++) printf("%d",c[j]);
    printf("\n");
    return 0;
 } 

 

posted on 2019-08-24 17:06  cltt  阅读(129)  评论(0编辑  收藏  举报

导航