美团17年A论试题第一题

链接: https://www.nowcoder.com/acm/contest/6/A
来源:牛客网

有一个大水缸,里面水的温度为T单位,体积为C升。另有n杯水(假设每个杯子的容量是无限的),每杯水的温度为t[i]单位,体积为c[i]升。
现在要把大水缸的水倒入n杯水中,使得n杯水的温度相同,请问这可能吗?并求出可行的最高温度,保留4位小数。
注意:一杯温度为t1单位、体积为c1升的水与另一杯温度为t2单位、体积为c2升的水混合后,温度变为(t1*c1+t2*c2)/(c1+c2),体积变为c1+c2。

输入描述:

第一行一个整数n, 1 ≤ n ≤ 10^5
第二行两个整数T,C,其中0 ≤ T ≤ 10^4, 0 ≤ C ≤ 10^9
接下来n行每行两个整数t[i],c[i]
0 ≤ t[i], c[i] ≤ 10^4

输出描述:

如果非法,输出“Impossible”(不带引号)否则第一行输出“Possible"(不带引号),第二行输出一个保留4位小数的实数表示答案。

样例解释:往第二杯水中倒0.5升水
往第三杯水中到1升水
三杯水的温度都变成了20
 
 
#include <iostream>
#include<iomanip>
using namespace std;
#define maxn 10000

int main()
{
    int t[maxn],c[maxn];
    int n,T;
    long int C;
    cin>>n;
    cin>>T;
    cin>>C;
    double tempmax=0,tempmin=10005;
    double c_all=0,ct_all=0;
    for(int i=0;i<n;i++)
    {
        cin>>t[i];
        cin>>c[i];
        ct_all=ct_all+c[i]*t[i];
        c_all=c_all+c[i];
        if (t[i]>tempmax) tempmax=t[i];
        if (t[i]<tempmin) tempmin=t[i];
    }
    if(T>=tempmax)
    {
        double tt=(T*C+ct_all)/(C+c_all);
        if(tt>=tempmax)
        {
            cout<<"Possible"<<endl;
            cout<<fixed<<setprecision(4)<<tt;
        }
        else cout<<"Impossible"<<endl;
    }
    else if(T<=tempmin)
    {
        double tt=(T*C+ct_all)/(C+c_all);
        if(tt<=tempmin)
        {
            cout<<"Possible"<<endl;
            cout<<fixed<< setprecision(4)<<tempmin;
        }
        else cout<<"Impossible"<<endl;
    }
    else cout<<"Impossible"<<endl;
}

猜你喜欢

转载自blog.csdn.net/hexiquan123/article/details/80631959
今日推荐