看懂上图就出题了,注意给出的数字很长,可以用字符数组存放 。
代码如下:
1 #include<iostream> 2 #include<cmath> 3 #include<cstring> 4 5 using namespace std; 6 7 int main() 8 { 9 int n, s; 10 int i; 11 char str[40]; 12 while(cin >> str, strcmp(str, "0")) 13 { 14 s=0; 15 n=strlen(str); 16 for (i=0; i<n; i++) 17 { 18 s+=(str[i]-'0')*(pow(2, n-i)-1); 19 } 20 cout << s << endl; 21 } 22 return 0; 23 }