[UVA 545] Heads 題解

題意:給出 \(n\) 題目讓你求 \(2\)\(-n\) 次方,即 \(2^{-n}\)c++

我不會算負次方。怎麼辦, Bing一下啊!spa

一個數的負次方等於這個數的多少次方的倒數。code

按照定義,咱們來分別肯定冪的值。ci

首先是冪是負數。it

按照定義,咱們能夠推得 \(e=-n\times\log(2)\)class

這樣就能夠把數據給格式化輸出,最後只要計算出 \(10^{-n\times\log(2)-e}\) 而後輸出 \(e\) 次方便可。im

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);
    }
}
相關文章
相關標籤/搜索