#UVa:701-The Archeologists’ Dilemma

可以利用數學式子先推:2^E = N*10^k + C (C為正整數) (省略C)-> N*10^k (同取log2)-> log2(N) + k*log2(10) 故找log2(N) + k*log2(10)log2(N+1) + k*log2(10)之值之中是否具有整數,若有即是解。

P.S. (log2(N+1) + k*log2(10))絕對不會為整數,在k>=1的情況下。

C++(0.136)

/*******************************************************/
/* UVa 701 The Archeologists' Dilemma                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/05/18                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int digit( int x ){
  int value = 0;
  while(x){
    x /= 10;
    value++;
  }
  return value;
}

int main(){
  int N, k, lower, upper;
  while( scanf( "%d", &N ) != EOF ){
    k = digit(N)+1;
    while(1){
      lower = (int)(log2(N)+k*log2(10));
      upper = (int)(log2(N+1)+k*log2(10));
      if( lower != upper ){
        printf( "%d\n", upper );
        break;
      }
      k++;
    }
  }
  return 0;
}

發表迴響