#include<stdio.h>
//求a的b次方函数
unsigned ppow(unsigned a, unsigned b) {
unsigned cc = 1;
while (b) {
cc*=a;
b--;
}
return cc;
}
int main()
{
unsigned long long n = 0;
unsigned i, k=0, count = 0;
printf("请输入2进制数:");
scanf_s("%lld", &n); //用scanf()在Visual Studio 2019 会警告返回值被忽略
while (n) {
i = n % 10; //这个说错了,2和10都可以,用10会比较好
if (i) { //判断i是否为0
count += ppow(2, k);
}
k++; //k做为2的次方数,每次n /= 10 就+1
n /= 10;
}
printf(" 转成10进制= %d",count);
return 0;
}