Problem A: 二进制整数转十进制
Description
给出一个二进制的非负整数x,x<232,把它转换成十进制数输出。
Input
输入为多行,每行一个二进制非负整数x。
Output
每行输出x对应的十进制数值。
Sample Input
0
1
01
10
11
100001
1111111111111111
Sample Output
0
1
1
2
3
33
65535
HINT
注意数据范围!!!
答案:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char s[32];
int i;
int length;
unsigned ans;
while(scanf("%s",s) != EOF)
{
length = strlen(s);
ans = 0;
for(i=0; i<length; ++i)
{
ans =ans*2+(s[i]-48);
}
printf("%u\n",ans);
}
return 0;
}