UVa 701 The Archeologists' Dilemma

枚举需要的位数2ans > n * 10&&  2ans < (n + 1) * 10i , i 的初值为log10(n) + 2。而符合条件的一个整数ans就是答案。

log2(2ans) = ans, log2(n * 10i) = log2(n) + i * log2(10),log2((n  + 1)* 10i) = log2(n + 1) + i * log2(10)。/* File: 701.cpp Author: ACboy Date: 2010-4-21 Result: AC Descripition: UVa 701 The Archeologists' Dilemma */ #include <iostream> #include <cmath> using namespace std; double log(double a, double n) { return log10(n) / log10(a); } int main() { double a, b, n; #ifndef ONLINE_JUDGE freopen("701.txt", "r", stdin); #endif while (scanf("%lf", &n) == 1) { int ok = 0; for (long i = (long)log10(n) + 2; !ok; i++) { a = log(2, n) + i * log(2, 10); b = log(2, n + 1) + i * log(2, 10); for (long j = (long)a; j < b && !ok; j++) { if (j >= a && j < b) { printf("%ld/n", j); ok = 1; break; } } } } return 0; }

你可能感兴趣的:(UVa 701 The Archeologists' Dilemma)