Skip to content

Instantly share code, notes, and snippets.

@ajuanhuang
Created December 7, 2012 06:59
Show Gist options
  • Save ajuanhuang/4231342 to your computer and use it in GitHub Desktop.
Save ajuanhuang/4231342 to your computer and use it in GitHub Desktop.
二进制十进制转换
#include<stdio.h>
#include<math.h>
int main()
{
int i=0,n,k,s=0,j=0,a[1000];
char m;
while(scanf("%c%d",&m,&n)!=EOF)
{
getchar();
if(m=='B')
{ i=0;
while(n
>0)
{
a[i]=n%10;
n=n/10;
s=s+a[i]*pow(2,i);
i++;
}
printf("%d\n",s);
}
if(m=='D')
{ j=0;
while(n>0)
{
a[j]=n%2;
n=n/2;
j++;
}
if(j<8)
{
for(i=0;i<8-j;i++)
printf("0");
for(i=j-1;i>=0;i--)
printf("%d",a[i]);
}
else
for(k=0;k<j;k++)
printf("%d",a[k]);
printf("\n");
}
}return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment