#UVa:575-Skew Binary

善用pow函式即可得解。

C++(0.008)

/*******************************************************/
/* UVa 575 Skew Binary                                 */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/03/17                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
#define ERROR 1e-8
using namespace std;

int main(){
  string n;
  int sum;

  while( cin >> n && n != "0" ){
    sum = 0;
    for( int i = n.length()-1 ; i >= 0 ; i-- )
      sum += (n[i]-'0') * ((int)( pow( 2.0, n.length()-i ) + ERROR ) - 1);
    printf( "%d\n", sum );
  }
  return 0;
}

發表迴響