C/C++ Programming Learning-Week 3 ② The elephant drinks water

Topic link

Title description

In class, the teacher asked Xiao Suan Suan and the students a question:

An elephant is thirsty and needs to drink 20 liters of water to quench his thirst, but now there is only a small bucket with a depth of h cm and a bottom radius of r cm (h and r are both integers). Ask the elephant how many buckets of water should he drink at least to quench his thirst.

Xiao Suan Suan wants you to do the math.

Input format The
input has one line: two integers in the line, separated by a space, respectively representing the depth h (1≤h≤100) and the bottom radius r (1 ≤ r ≤ 100) of the small drum, and the unit is cm.

Output format
Output a line containing an integer, indicating the number of buckets the elephant must drink at least.

Tips
If a drum has a depth of h cm and a bottom radius of r cm, then it can hold at most π × r × r × h cubic centimeters of water. (Suppose π=3.14159) 1 liter=1000 milliliters; 1 milliliter = 1 cubic centimeter.

Sample Input

23 11

Sample Output

3

Ideas

First calculate the volume of a small bucket of water, divide 20 liters by this volume, and round up, pay attention to the unit conversion. ceil() is the round-up function.

C language code:

#include<stdio.h>
int main()
{
    
    
	int cnt = 1, h, r;
    scanf("%d %d",&h, &r);
	double v = 3.14159 * h * r * r;
	while((cnt * v) < 20000)
		cnt ++;
	printf("%d", cnt);
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int h, r;
	while(cin >> h >> r)
	{
    
    
		double v = 3.14159 * r * r * h;
		cout << ceil(20.0 * 1000 / v) << endl;	//向上取整
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112859896