zoukankan      html  css  js  c++  java
  • 蓝桥杯基础练习题4(16进制转8进制)2

    /*
    问题描述
      给定n个十六进制正整数,输出它们对应的八进制数。

    输入格式
      输入的第一行为一个正整数n (1<=n<=10)。
      接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过20。

    输出格式
      输出n行,每行为输入对应的八进制正整数。

      【注意】
      输入的十六进制数不会有前导0,比如012A。
      输出的八进制数也不能有前导0。

    样例输入
      2
      39
      123ABC

    样例输出
      71
      4435274

      【提示】
      先将十六进制数转换成某进制数,再由某进制数转换成八进制。
    */
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    int main(void){
    // printf("hello world!");

    int NUM_HEX; //16进制数个数
    int NUM_OCT; //8进制数个数


    int tag; //循环标志
    int num_tag;
    //int BIN_TAG=0; //整体二进制码输入标志
    int bin_tag=0; //单个二进制码输入标志
    int tempInt; //临时变量


    char Hex[20]; //定义一个长度为20的数组,为之后的操作做准备
    char Oct[80]; //16进制数二进制码的长度/4*3就是8进制数二进制码的长度
    char Bin0[90];
    char Bin1[90];
    char Bin2[90];
    char Bin3[90];
    char Bin4[90];
    char Bin5[90];
    char Bin6[90];
    char Bin7[90];
    char Bin8[90];
    char Bin9[90];
    memset( Bin0, '', sizeof(Bin0) );
    memset( Bin1, '', sizeof(Bin1) );
    memset( Bin2, '', sizeof(Bin2) );
    memset( Bin3, '', sizeof(Bin3) );
    memset( Bin4, '', sizeof(Bin4) );
    memset( Bin5, '', sizeof(Bin5) );
    memset( Bin6, '', sizeof(Bin6) );
    memset( Bin7, '', sizeof(Bin7) );
    memset( Bin8, '', sizeof(Bin8) );
    memset( Bin9, '', sizeof(Bin9) );

    char *BIN_CODE; //根据BIN_TAG确定使用哪一组二进制数组
    BIN_CODE=Bin0;

    div_t div_temp;

    scanf("%d", &NUM_HEX); //确定输入正整数的个数
    NUM_OCT = NUM_HEX; //8进制数个数与16进制数相同

    for(tag=0; tag<20; ++tag){
    Hex[tag]= '0'; //全部初始设置为0,防止不确定数据生成
    }
    for(tag=0; tag<80; ++tag){
    Oct[tag]= '0'; //全部初始设置为0,防止不确定数据生成
    }

    /***TODO 计划每转换一组数据就将数据存储在Oct数组中,然后重新初始化
    Hex数组和Oct数组,再对Hex数组反复操作 将得到的数据分别存储在Binn数组中。。
    最后输出

    */


    for(tag=0; tag<NUM_HEX; ++tag){
    fflush(stdin); //清除缓存区
    gets(Hex); //输入NUM_HEX个16进制数
    //printf("%d",strlen(Hex));

    if(tag==0) BIN_CODE=Bin0;
    if(tag==1) BIN_CODE=Bin1;
    if(tag==2) BIN_CODE=Bin2;
    if(tag==3) BIN_CODE=Bin3;
    if(tag==4) BIN_CODE=Bin4;
    if(tag==5) BIN_CODE=Bin5;
    if(tag==6) BIN_CODE=Bin6;
    if(tag==7) BIN_CODE=Bin7;
    if(tag==8) BIN_CODE=Bin8;
    if(tag==9) BIN_CODE=Bin9;
    for(num_tag=0; num_tag<strlen(Hex); ++num_tag){
    switch(Hex[num_tag]) {
    case '0':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case '1':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case '2':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case '3':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case '4':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case '5':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case '6':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case '7':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='0';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case '8':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case '9':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case 'A':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case 'B':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='0';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case 'C':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case 'D':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='0';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;
    case 'E':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='0';
    BIN_CODE[tempInt+4]='';
    break;
    case 'F':
    tempInt=num_tag*4;
    BIN_CODE[tempInt]='1';
    BIN_CODE[tempInt+1]='1';
    BIN_CODE[tempInt+2]='1';
    BIN_CODE[tempInt+3]='1';
    BIN_CODE[tempInt+4]='';
    break;

    default: printf("error input ");
    }
    //++bin_tag;
    }
    //BIN_TAG++; //每输入一次改变一次保存二进制数组的位置
    }
    /*
    gets(Hex);
    printf("%d",strlen(Hex));
    */
    //puts(Bin0);
    /*
    for(tag = 0; tag < NUM_HEX; ++tag){
    if(tag==0) BIN_CODE=Bin0;
    if(tag==1) BIN_CODE=Bin1;
    if(tag==2) BIN_CODE=Bin2;
    if(tag==3) BIN_CODE=Bin3;
    if(tag==4) BIN_CODE=Bin4;
    if(tag==5) BIN_CODE=Bin5;
    if(tag==6) BIN_CODE=Bin6;
    if(tag==7) BIN_CODE=Bin7;
    if(tag==8) BIN_CODE=Bin8;
    if(tag==9) BIN_CODE=Bin9;

    strcpy(Oct, BIN_CODE);
    //TEST
    puts(Bin0);
    }
    */
    /*
    strcpy(Oct, Bin0);
    puts(Oct);
    */



    /*******TODO
    一个16进制数转成的2进制必然是4的整数倍
    转换成8进制的对应代码需要为3的整数倍
    如何确定开头?
    如何在开头正确地添0并将每一位正确地后移?
    为防止数据覆盖,先从最后一位开始后移,第一位移动时,在原来的位置覆盖‘0’.
    怎么确定移动的个数呢?
    如果生成的二进制码能够被3整除,则不需要移动,如果不能,则需要后移的个数为3-(整个数组长除以3的余数)
    */


    for(tag=0; tag<NUM_HEX; ++tag){
    if(tag==0) BIN_CODE=Bin0;
    if(tag==1) BIN_CODE=Bin1;
    if(tag==2) BIN_CODE=Bin2;
    if(tag==3) BIN_CODE=Bin3;
    if(tag==4) BIN_CODE=Bin4;
    if(tag==5) BIN_CODE=Bin5;
    if(tag==6) BIN_CODE=Bin6;
    if(tag==7) BIN_CODE=Bin7;
    if(tag==8) BIN_CODE=Bin8;
    if(tag==9) BIN_CODE=Bin9;

    //printf("%d ", strlen(BIN_CODE));
    div_temp = div(strlen(BIN_CODE), 3);
    tempInt = 3 - div_temp.rem; //需要后移的位数
    //printf(" the number needs to put back is: %d ", tempInt);

    //printf("BEFORE the length of BIN_CODE is: %d ", strlen(BIN_CODE));
    for(num_tag = strlen(BIN_CODE)-1; num_tag >= 0; --num_tag) { //循环后移
    *(BIN_CODE+num_tag+tempInt) = *(BIN_CODE+num_tag);

    }
    //printf("NOW the length of BIN_CODE is: %d ", strlen(BIN_CODE));

    //前置0
    for(num_tag=0; num_tag < tempInt; ++num_tag){
    *(BIN_CODE+num_tag) = '0';
    }
    //puts(BIN_CODE);


    }

    for(tag=0; tag<NUM_HEX; ++tag){ //10个二进制数组
    if(tag==0) BIN_CODE=Bin0;
    if(tag==1) BIN_CODE=Bin1;
    if(tag==2) BIN_CODE=Bin2;
    if(tag==3) BIN_CODE=Bin3;
    if(tag==4) BIN_CODE=Bin4;
    if(tag==5) BIN_CODE=Bin5;
    if(tag==6) BIN_CODE=Bin6;
    if(tag==7) BIN_CODE=Bin7;
    if(tag==8) BIN_CODE=Bin8;
    if(tag==9) BIN_CODE=Bin9;


    for(num_tag=0; num_tag<strlen(BIN_CODE); num_tag+=3) {
    //printf("%d", num_tag);
    if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '0'){}
    if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '1'){printf("1");}
    if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '0'){printf("2");}
    if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '1'){printf("3");}
    if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '0'){printf("4");}
    if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '1'){printf("5");}
    if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '0'){printf("6");}
    if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '1'){printf("7");}
    }
    printf(" ");
    }




















    /*
    puts(Bin0);
    puts(Bin1);
    puts(Bin2);
    puts(Bin3);
    puts(Bin4);
    puts(Bin5);
    puts(Bin6);
    puts(Bin7);
    puts(Bin8);
    puts(Bin9);
    */


    return 0;
    }

  • 相关阅读:
    Superwebsocket 模拟微信聊天室
    python杂记-6(time&datetime模块)
    python杂记-5(装饰器)
    python杂记-4(迭代器&生成器)
    python杂记-3(购买商品)
    python杂记-2(python之文件)
    python杂记-1(os模块)
    淘宝链接中的spm参数
    分布式Web服务器架构(转)
    asp.net网站增加虚拟目录,用来访问网站图片。
  • 原文地址:https://www.cnblogs.com/jzl123/p/6292139.html
Copyright © 2011-2022 走看看