题目描述
有一个大水缸,里面水的温度为T单位,体积为C升。另有n杯水(假设每个杯子的容量是无限的),每杯水的温度为t[i]单位,体积为c[i]升。
现在要把大水缸的水倒入n杯水中,使得n杯水的温度相同,请问这可能吗?并求出可行的最高温度,保留4位小数。
注意:一杯温度为t1单位、体积为c1升的水与另一杯温度为t2单位、体积为c2升的水混合后,温度变为
(
t
1
∗
c
1
+
t
2
∗
c
2
)
/
(
c
1
+
c
2
)
(t1*c1+t2*c2)/(c1+c2)
(t1∗c1+t2∗c2)/(c1+c2),体积变为c1+c2
思路:
大水缸里的水温要么比最热的杯子热,要么比最冷的杯子冷。
- 如果是前者,需要将所有的水倒到每个杯子里,使所有杯子温度相等,并使最终温度大于等于一开始最热的杯子。
- 如果是后者,需要将部分水倒到某些杯子里,使所有杯子温度都等于一开始最冷的杯子。
如果是情况1,将所有水混合后的温度,一定大于等于初始最热的杯子,且这个温度就是结果。
如果是情况2,将所有水混合后的温度,一定小于等于初始最冷的杯子,且初始最冷的杯子的温度就是结果。
题目链接:https://ac.nowcoder.com/acm/problem/13228
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
double sumt = 0, sumc = 0;
double minx = 2e9, maxx = -1;
double ans;
double T, C;
int main()
{
double t, c;
int i, j, n;
cin >> n;
cin >> T >> C;
sumt += T * C;
sumc += C;
for (i = 1; i <= n; ++i)
{
cin >> t >> c;
minx = min(minx, t);
maxx = max(maxx, t);
sumt += t * c;
sumc += c;
}
ans = (double)sumt / sumc;
if (ans <= minx)
{
cout << "Possible" << endl;
printf("%.4lf\n", minx);
}
else if (ans >= maxx)
{
cout << "Possible" << endl;
printf("%.4lf\n", ans);
}
else
{
cout << "Impossible" << endl;
}
return 0;
}