问题描述
编写一个程序,输入一个二进制的字符串(长度不超过32),然后计算出相应的十进制整数,并把它打印出来。
输入格式:输入为一个字符串,每个字符都是’0’或’1’,字符串的长度不超过32。
输出格式:输出一个整数。
输入输出样例
输入格式:输入为一个字符串,每个字符都是’0’或’1’,字符串的长度不超过32。
输出格式:输出一个整数。
输入输出样例
样例输入
1101
样例输出
13
代码:
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
char s[32];
stringstream str;
cin>>s;
int a;
int i = strlen(s) - 1;
int c = 1;
long long sum = 0;
while(i >= 0)
{
str<<s[i];
str>>a;
sum = sum + a * c;
c = c * 2;
i--;
str.clear();
}
cout<<sum;
return 0;
}