C语言蓝桥杯 16进制转8进制 求解
#include<stdio.h>#include<string.h>
main()
{
char hex,oxc,olc;
int n,i,j,o=0,temp,num=0;
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
gets(hex);
strupr(hex);
}
for(i=0;i<n;i++)
{
for(j=0;j<strlen(hex);j++)
{
switch(hex)
{
case '0':strcpy(&oxc,"0000");break;
case '1':strcpy(&oxc,"0001");break;
case '2':strcpy(&oxc,"0010");break;
case '3':strcpy(&oxc,"0011");break;
case '4':strcpy(&oxc,"0100");break;
case '5':strcpy(&oxc,"0101");break;
case '6':strcpy(&oxc,"0110");break;
case '7':strcpy(&oxc,"0111");break;
case '8':strcpy(&oxc,"1000");break;
case '9':strcpy(&oxc,"1001");break;
case 'A':strcpy(&oxc,"1010");break;
case 'B':strcpy(&oxc,"1011");break;
case 'C':strcpy(&oxc,"1100");break;
case 'D':strcpy(&oxc,"1101");break;
case 'E':strcpy(&oxc,"1110");break;
case 'F':strcpy(&oxc,"1111");break;
default :break;
}
o+=4;
}
}
for(i=0;i<n;i++)
{
if(strlen(oxc)%3)
o=1+(strlen(oxc)-1)/3;
else
o=1+strlen(oxc)/3;
olc='\0';
for(j=strlen(oxc)-1;j>=0;j--)
{
temp=3;
while(temp>0){
if(oxc=='1')
{
switch(temp)
{
case 3 :num+=1;break;
case 2 :num+=2;break;
case 1 :num+=4;break;
default:break;
}
}
--temp;
if(temp>0) --j;
}
olc=num+'0';
num=0;
--o;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<strlen(olc);j++)
{
if(olc!='0')
printf("%c",olc);
}
printf("\n");
}
return 0;
}/*帮我看看 哪里错了 只能算一个 16进制数 多了就不行了*/
帮忙给我提提速 #include<stdio.h>
#include<string.h>
main()
{
char hex,oxc,olc;
int n,i,j,o,temp,num=0;
scanf("%d",&n);
fflush(stdin);
for(i=0;i<n;i++)
{
scanf("%s",hex);
strupr(hex);
}
for(i=0;i<n;i++)
{
o=0;
for(j=0;j<strlen(hex);j++)
{
switch(hex)
{
case '0':strcpy(&oxc,"0000");break;
case '1':strcpy(&oxc,"0001");break;
case '2':strcpy(&oxc,"0010");break;
case '3':strcpy(&oxc,"0011");break;
case '4':strcpy(&oxc,"0100");break;
case '5':strcpy(&oxc,"0101");break;
case '6':strcpy(&oxc,"0110");break;
case '7':strcpy(&oxc,"0111");break;
case '8':strcpy(&oxc,"1000");break;
case '9':strcpy(&oxc,"1001");break;
case 'A':strcpy(&oxc,"1010");break;
case 'B':strcpy(&oxc,"1011");break;
case 'C':strcpy(&oxc,"1100");break;
case 'D':strcpy(&oxc,"1101");break;
case 'E':strcpy(&oxc,"1110");break;
case 'F':strcpy(&oxc,"1111");break;
default :break;
}
o+=4;
}
}
for(i=0;i<n;i++)
{
if(strlen(oxc)%3)
o=1+strlen(oxc)/3;
else
o=strlen(oxc)/3;
olc='\0';
for(j=strlen(oxc)-1;j>=0;j--)
{
temp=3;
while(temp>0){
if(oxc=='1')
{
switch(temp)
{
case 3 :num+=1;break;
case 2 :num+=2;break;
case 1 :num+=4;break;
default:break;
}
}
--temp;
if(temp>0) --j;
}
olc=num+'0';
num=0;
--o;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<strlen(olc);j++)
{
if(olc!='0')
printf("%c",olc);
}
printf("\n");
}
return 0;
} 呵呵 二个逗比 叫你们老帮忙的 怎么来顶帖的了 我当时是想用栈,这样子就得把字符串用数组表示,每行输入的16进制数字,存在属于在一的那一行,再将这一行的字符串换成10进制数字,入栈,再将这行的十进制数字换成八进制。(我看了锦囊,直接将16进制转化为2进制,再将2进制转化为8进制应该是最快的。){:10_254:}
页:
[1]