倒水(newcoder)

题目链接:倒水 (nowcoder.com)

题目描述:

有一个大水缸,里面水的温度为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

 

示例:

输入:
3
10 2
20 1
25 1
30 1

输出:
Possible
20.0000

 

代码:

C:

#include<stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    int T;
    long long int C;
    scanf("%d %lld", &T, &C);
    int t[100001], c[100001];
    double max = 0, min = 10006, s1 = T * C, s2 = C, x;
    for (int i = 0;i < n;i++)
    {
        scanf("%d %d", &t[i], &c[i]);
        if (t[i] > max)
            max = t[i];
        if (min > t[i])
            min = t[i];
        s1 += t[i] * c[i];
        s2 += c[i];
    }
    x = s1 / s2;
    if (x >= max)
        printf("Possible\n%.4lf", x);
    else if (x <= min)
        printf("Possible\n%.4lf", min);
    else
        printf("Impossible");
    return 0;
}

 

posted @ 2021-01-31 02:06  Carrout  阅读(288)  评论(0编辑  收藏  举报