Pankaj likes to eat Ice cream when he is working late into the night. Today has been yet…
- User
Pankaj likes to eat Ice cream when he is working late into the night. Today has been yet another long day for Pankaj. So, he wants to eat ice cream now. He opens the fridge and sees that he has 2 types of containers holding the ice cream. The first container is a cone with radius r1 and height h1. There is also a hemisphere on the top of the cone which has the same radius. The other container is a cylindrical with radius r2 and height h2. Pankaj wants to know the amount (volume) of ice cream in both the containers. Since Pankaj is tired after coding all day, you have to help him with this task. Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case consists of a single line having the r1, h1, r2 and h2. Each value is given upto 2 decimal places. See example for more information.
Output
For each test case, output a single line containing the volumes of the two containers separated by space. The answer is considered correct if it is correct upto 6 decimal places.
Constraints
1 ≤ T ≤ 100 0 < r1, h1, r2, h2 ≤ 100
Example Input: 2 1.00 1.00 1.00 1.00 3.02 7.23 5.20 6.00
Output: 3.141592654 3.141592654 126.739919445 509.691992118
Assistant
We need to read T test cases. For each test case, we have r1, h1, r2, h2. For container1: It is a cone with radius r1 and height h1, plus a hemisphere on top with same radius r1. So total volume = volume of cone + volume of hemisphere. Hemisphere volume is half of sphere volume: (2/3)π r1^3? Actually volume of sphere = (4/3)π r^3. Hemisphere = (2/3)π r^3. Cone volume = (1/3)π r1^2 h1. So total V1 = π r1^2 h1 / 3 + (2/3)π r1^3 = (π/3)(r1^2 h1 + 2 r1^3) = (π/3) * r1^2 (h1 + 2 r1). Could also compute separately.
For container2: Cylinder volume = π r2^2 h2.
We need to output both volumes to 6 decimal places. The example shows for 1.0 1.0 1.0 1.0: V1 = π/3(11+21) = π/3 (1+2)=π; so 3.141592654; V2 = π11 = π; same. So they printed with 9 decimals but only need 6 decimal places. So we can compute using double and output with setprecision(9) maybe but 6 is fine.
Constraints up to 100, two decimals. Using double should be fine.
We need to read floating point numbers with 2 decimal places. In C++, use float/double reading from cin. We'll output using fixed and setprecision(10) to ensure 6 decimal places.
But careful: The formula uses r1^3, so be careful about overflow? up to 100^3 = 1e6, double is fine.
Edge cases: r1, h1, r2, h2 >0. So all positive.
Implement: loop T times, read r1, h1, r2, h2 as double. Compute V1 = (M_PI * r1 * r1 * h1) / 3.0 + (2.0/3.0) * M_PI * r1 * r1 * r1? Actually hemisphere volume = (2.0/3.0) * M_PI * r1 * r1 * r1? Wait (2/3)π r^3 is correct. So compute:
double V1 = (M_PI * r1 * r1 * h1) / 3.0 + (2.0/3.0) * M_PI * r1 * r1 * r1;
Alternatively: V1 = M_PI * r1 * r1 * (h1/3.0 + (2.0/3.0)r1) = M_PI * r1 * r1 * (h1 + 2r1) / 3.0.
Better compute as: V1 = M_PI * r1 * r1 * (h1 + 2.0 * r1) / 3.0;
V2 = M_PI * r2 * r2 * h2.
We need to output V1 and V2 separated by space.
Set output format:
cout << fixed << setprecision(6) << V1 << " " << V2 << endl; But example shows 9 decimals? The output specification: answer correct upto 6 decimal places. So we can output 6 or more. Usually printing 6 decimal places is fine, but sometimes judges accept if answer within absolute or relative error. Since they say "correct upto 6 decimal places", printing with 6 decimal places should be fine. However, to be safe we can output 9 or 10 decimals, but they compare with tolerance likely. Typically they compare with 1e-6 absolute or relative. We'll output with high precision like 10.
We need to include , , . Use M_PI from maybe not defined in some compilers? On some, need to define _USE_MATH_DEFINES. We can define it manually: const double PI = acos(-1.0);. That is safer. We'll compute PI = acos(-1.0).
Now implement solve().
Also note input T integer. Then each line four doubles. Use cin.
Check example: second case: r1=3.02, h1=7.23, r2=5.20, h2=6.00. Compute V1: π * 3.02^2 * (7.23 + 23.02) /3 = π * 9.1204? Actually 3.02^2 = 9.1204? 3.023.02=9.1204 indeed. (7.23+6.04)=13.27. Multiply: 9.120413.27=121.054? Let's compute: 9.120413.27 = 9.120413 + 9.12040.27 = 118.5652 + 2.462508 = 121.027708. Then divided by 3 = 40.34256933. Times π = 126.739... matches output 126.739919445. So okay.
For V2: π * 5.20^2 * 6 = π * 27.04 *6 = 162.24π = 509.691992... matches.
Now code. We'll define const double PI = acos(-1.0);
Make sure to include for setprecision.
Potential pitfalls: Floating point precision. Use double.
Now produce final answer.
We'll output each test case.
Implementation:
#include #include #include
using namespace std;
const double PI = acos(-1.0);
int main() { int T; cin >> T; cout << fixed << setprecision(10); // 10 decimals while (T--) { double r1, h1, r2, h2; cin >> r1 >> h1 >> r2 >> h2; double V1 = PI * r1 * r1 * (h1 + 2.0 * r1) / 3.0; double V2 = PI * r2 * r2 * h2; cout << V1 << " " << V2 << endl; } return 0; }
But note: they might want exactly 6 decimals? The output statement says "answer is considered correct if it is correct upto 6 decimal places." So printing with 10 decimals is fine, but might cause presentation issues if they compare exactly? Usually they compare with tolerance. So fine.
Alternatively, we can use setprecision(6). But we could use 9 to match example. I'll use 10.
Now final answer. ```cpp #include #include #include
using namespace std;
const double PI = acos(-1.0);
int main() { int t; cin >> t; // set output to fixed point with enough digits (6 decimal places required) cout << fixed << setprecision(10); // 10 ensures at least 6 digits after decimal while (t--) { double r1, h1, r2, h2; cin >> r1 >> h1 >> r2 >> h2; double volume1 = PI * r1 * r1 * (h1 + 2.0 * r1) / 3.0; double volume2 = PI * r2 * r2 * h2; cout << volume1 << " " << volume2 << endl; } return 0; }