洛谷题库—入门1—顺序结构—B2029 大象喝水

 一、题目要求

  二、代码

#include <iostream> //标准库
#include <string>   //字符串
#include <sstream>  //字符串流
#include <vector>   //盒子
#include <cmath>    //数学库

//命名空间
using namespace std;

//切分字符串
vector<string> split(string str,char ch)
{
	//创建字符串盒子
	vector<string> results;
	//将字符串转为字符串流
	stringstream ss(str);
	//创建字符串临时存放点
	string temp;
	//循环切分,并将每一次切分的结果放入盒子
	while(getline(ss,temp,ch))
		{
			results.push_back(temp);
		}
	//返回结果
	return results;		
}

//主函数
int main()
{
	//创建变量字符串
	string str;
	//从键盘获取字符串
	getline(cin,str);
	//创建字符串盒子	
	vector<string> results;
	//切分字符串,并依次放入盒子
	results = split(str,' ');
	//从盒子里拿出字符串1
	string str1 = results[0];
	//将字符串1转为int
	int h =stoi(str1);
	//从盒子里拿出字符串2
	string str2 = results[1];
	//将字符串2转为int
	int r =stoi(str2);
	
	//定义常量
	const int x = 20;
	//定义常量
	const int p = M_PI;
	
	//计算体积
	int v = p*r*r*h/1000;
	
	//计算
	int t = x / v;
	
	//判断是否加1
	if ( x % v != 0)
    {
		t = t + 1;
	}
	
	//输出结果
	cout << t;	
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ssismm/article/details/128730009