CodeForces 1010A

題目鏈接: http://codeforces.com/contest/1010/problem/A
這道題注意下輸出精度:#include ……cout << fixed << setprecision(10)<< weight << endl;
還有就是無解的情況:起落時,1油帶動的重量>1,題目提示的是油不超過1e+9,實際上就是根據數學計算得出的條件:a,b不可以爲1(除數不能爲0)
現在發現其實CodeForces上可以查看別人提交的代碼,可以好好學習一下!

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int n,m,i;
    long double weight=0,total=0,temp;
    int a[1005];
    int b[1005];
    cin>>n;
    cin>>m;
    total=m;
    for(i=0;i<n;i++){
        cin>>a[i];
        if(a[i]==1){
            cout<< -1 <<endl;
            return 0;
        }
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        if(b[i]==1){
            cout<< -1 <<endl;
            return 0;
        }
    }
    temp=total/(b[0]-1);
    total+=temp;
    weight+=temp;
    for(i=n-1;i>0;i--){
        temp = total/(a[i]-1);
        total+=temp;
        weight+=temp;
        temp = total/(b[i]-1);
        total+=temp;
        weight+=temp;
    }
    temp = total/(a[0]-1);
    total+=temp;
    weight+=temp;
    cout << fixed << setprecision(10)<< weight << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章