题目描述
已知一个只包含 0 和 1 的二进制数,长度不大于 10 ,将其转换为十进制并输出。
输入描述输入一个二进制整数n,其长度不大于10
输出描述输出转换后的十进制数, 占一行
样例输入110
样例输出6
提交代码
/* students please write your program here */ #include <iostream> #include <cstring> using namespace std; int main() { char a[10]; int n; cin >> a; n = strlen(a); int sum = 0; for (int i = 0; i < n; i++) { sum = sum * 2 + (a[i]-'0'); } cout << sum << endl; }