大数十六进制转换成8进制
发布时间:2020-12-14 03:42:51 所属栏目:大数据 来源:网络整理
导读:说明: 问题描述 给定n个十六进制正整数,输出它们对应的八进制数。 输入格式 输入的第一行为一个正整数n (1=n=10)。 接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。 输出格式 输出n行
说明:
问题描述
给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
输入的第一行为一个正整数n (1<=n<=10)。
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出格式
输出n行,每行为输入对应的八进制正整数。
注意
输入的十六进制数不会有前导0,比如012A。
输出的八进制数也不能有前导0。
样例输入
2
39 123ABC
样例输出
71
4435274 #include"stdio.h" #include"stdlib.h" int zhuanhuan(short *b,char *c) { long shiliu_to_shi(char ch); short *temp; temp=(short *)malloc(sizeof(short)*400000);//c[0]是高位,转换完后temo[0]是高位,temp[0]到temp[n]采用从高位到低位 排列 long long num=0;//十六进制ch*中的计数器 int j=0; long long temp_num=0;//二进制数组中的计数器 while((j=shiliu_to_shi(c[num]))>=0) { //printf("j=%d ",j); int i; for(i=0;i<4;i++) { temp[temp_num+3-i]=(j>>i)&1; } temp_num=temp_num+4; num++; } temp[temp_num]=-1; //printf("j=%d num=%dn temp_num=%dn ",j,num,temp_num); num=0; //while(++num<=temp_num) // printf("%d",temp[num-1]); // printf("n"); num=0; b[num]=0; while(temp_num>0){ if(temp_num-3>=0) b[num]=temp[temp_num-3]*4; else b[num]=0; if(temp_num-2>=0) b[num]=b[num]+temp[temp_num-2]*2; if(temp_num-1>=0) b[num]=b[num]+temp[temp_num-1]; temp_num=temp_num-3; num++; } //printf("nb=n"); while(num-->0){ if(b[num]>0) break; } while(num>=0){ printf("%d",b[num]); num--; } printf("n"); return 1; } int shiliu_to_shi(char ch) { int temp=-2; if(ch<='9'&&ch>='1') temp=ch-'1'+1; else if(ch=='0') temp=0; else if(ch>='A'&&ch<='F') {temp=ch-'A'+10; } else if(ch==' ') return -1; return temp; } int main() {int zhuanhuan(short *b,char *c); char *ch[10]; for(int i=0;i<10;i++) {ch[i]=(char *)malloc(sizeof(char)*100000); } long num=0,i,j; char c; short *b=(short *)malloc(sizeof(short)*200000); scanf("%d%c",&num,&c); for(i=0;i<num;i++) { scanf("%s%c",ch[i],&c); } for(i=0;i<num;i++) { zhuanhuan(b,ch[i]); // printf("n"); //puts(ch[i]); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |