首页 > 代码库 > UVA 474 - Heads / Tails Probability(递推)

UVA 474 - Heads / Tails Probability(递推)

UVA 474 - Heads / Tails Probability

题目链接

题意:给定n,求出2?n的科学计数法

思路:水水的,n最多到100w,那么先递推预处理出答案,然后输出即可

代码:

#include <cstdio>
#include <cstring>

const int N = 1000001;
const double eps = 1e-9;
int n;
struct Ans {
    double a;
    int b;
} ans[N];

void build() {
    ans[1].a = 5; ans[1].b = 1;
    for (int i = 2; i < N; i++) {
	ans[i].a = ans[i - 1].a * 0.5;
	ans[i].b = ans[i - 1].b;
	if (ans[i].a < 1.0) {
	    ans[i].a *= 10;
	    ans[i].b++;
	}
    }
}

int main() {
    build();
    while (~scanf("%d", &n)) {
	printf("2^-%d = %.3lfe-%d\n", n, ans[n].a, ans[n].b);
    }
    return 0;
}