題目大意:輸入值求對應題目算法的結果。例:(10120skew = 1×(25 −1)+ 0×(24 −1)+ 1×(23 −1)+ 2×(22 −1)+ 0×(21 −1) = 31+ 0+ 7+ 6+ 0 = 44)
解題思路:直接計算,擔心超出範圍,用long long。字元串存輸入的值。
ac代碼:
#include <iostream>
#include <cstring>
using namespace std;
char st[1005];
long long sum, len;
int main()
{
while (scanf("%s", st)!=EOF && st[0]!='0'){
len = strlen(st);
sum = 0;
for (int i=len-1,n=2; i>=0; i--,n*=2)
sum += (st[i]-'0') * (n-1);
printf("%lld\n", sum);
}
return 0;
}