[UVA 545] Heads 题解
题意:给出 \(n\) 题目让你求 \(2\) 的 \(-n\) 次方,即 \(2^{-n}\) 。
我不会算负次方。怎么办, Bing一下啊!
一个数的负次方等于这个数的多少次方的倒数。
按照定义,我们来分别确定幂的值。
首先是幂是负数。
按照定义,我们可以推得 \(e=-n\times\log(2)\) 。
这样就可以把数据给格式化输出,最后只要计算出 \(10^{-n\times\log(2)-e}\) 然后输出 \(e\) 次方即可。
Talk is cheap, show me the Code.
#include <bits/stdc++.h>
using namespace std;
int main(){
int T, n;
cin>>T;
while(T--) {
cin>>n;
int e=floor(-n * log10(2));
printf("2^-%d = %.3lfE%d\n",n,pow(10,-n*log10(2)-e),e);
}
}