当前位置:编程学习 > C/C++ >>

uva 701 - The Archeologists' Dilemma

题目大意:给出x,求一个e,使得x * 10 ^ y ≤ 2 ^ e < (x + 1) * 10 ^ y。
 
解题思路:问题可以转换成log2(x) + y * log2(10) ≤ e < log2(x + 1) + y*log2(10),
然后枚举y,判断条件。
 
 
#include <stdio.h>  
#include <stdlib.h>  
#include <math.h>  
  
int main  () {  
    int x, y;  
    double l, r, d, a, b;  
    while (scanf("%d", &x) == 1) {  
        y = x;  
        int c = 0;  
        while (y) {  
            c++;  
            y /= 10;  
        }  
  
        l = log(x) / log(2);  
        r = log(x + 1) / log(2);  
        d = log(10) / log(2);  
  
        for (int i = c + 1; ; i++) {  
            a = l + i * d;  
            b = r + i * d;  
  
            if (ceil(a) <= floor(b)) {  
                printf("%.0lf\n", ceil(a));  
                break;  
            } else if (i == 1000000) {  
                    printf("no power of 2\n");  
                    break;  
            }  
        }  
    }  
    return 0;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,