1142: 二进制数的大小
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1571 Solved: 967
Submit Status Web Board
Description
输入三个2 进制的数,要求将这三个二进制数对应的十进制整数按从小到大的顺序输。
要求程序定义一个bToD()函数和一个main()函数,bToD() 函数的功能是将二进制数转化为十进制整数,其余功能在main()函数中实现。
int bToD(char str[])
{
//函数返回二进制数str对应十进制整数
}
Input
输入三个2 进制的数,用空格隔开。所有数据均为非负数,二进制数长度不超过30。
Output
将对应的十进制整数按从小到大的顺序输出
Sample Input
1101 110 1011
Sample Output
6 11 13
HINT
Source
#include<stdio.h>
#include<string.h>
#define N 50
int BTOD (char str[]);
int main ()
{
char str[N],str1[N],str2[N];
int a,b,c,temp;
//输入三个二进制数
scanf("%s%s%s",str,str1,str2);
a = BTOD(str);
b = BTOD(str1);
c = BTOD(str2);
if ( a > b )
{
temp=a;
a=b;
b=temp;
}
if (a > c )
{
temp=a;
a=c;
c=temp;
}
if ( b > c )
{
temp=b;
b=c;
c=temp;
}
//按从小到大的顺序输出三个整数
printf("%d %d %d",a,b,c);
return 0;
}
int BTOD (char str[])
{
int i, d ;
d = 0;
for ( i = 0 ; str[i]!='\0';i++)
{
d = d*2 +(str[i]-'0'); //把每一位字符转换为对应的数字
}
return d;
}