在C ++中用N卢比可以购买的最大公升水
我们给了N卢比。目标是用钱购买尽可能多的水,其中水瓶的费率如下-
塑料瓶:1升卢比
玻璃瓶:B卢比1升
玻璃瓶:B卢比1升
现在,玻璃瓶的原价为BE卢比。回国后。
如果塑料瓶的成本仍低于BE,则只能购买塑料瓶。否则,购买NE/BE玻璃瓶,然后花在塑料瓶上。
输入值
N = 6, A = 5, B = 4, E = 3;
输出结果
Maximum litres of water: 3
说明-BE=1,1<ANE=3,我们以3卢比的价格购买了3个1升玻璃瓶
输入值
N = 10, A = 5, B = 10, E = 3;
输出结果
Maximum litres of water: 2
说明-BE=7,7>An/a=10/5=可以购买2个塑料瓶
以下程序中使用的方法如下
整数货币,瓶子,酒瓶和古怪的东西用于我们的费率和金额。
函数maxWater(intmny,intpb,intgb,intge)将所有值用作参数,并打印可以购买的水量。
可变的升数用于存储以升为单位的计算出的水量。
我们将pb作为玻璃瓶的新值(原始值-返回值)传递。
如果通过的gb值小于pb值,则购买(mny-ge)/gb玻璃瓶。
减去此金额以计算剩余资金,mny-=ltrs*gb
现在可以购买的塑料瓶为mny/pb。
如果传递的pb值大于pb值,则仅购买mny/pb塑料瓶。
两种情况下的打印结果均为否。瓶是没有的。升水。每瓶容量为1升。
示例
// CPP implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
void maxWater(int mny,int pb,int gb,int ge){
int litrs;
//如果购买玻璃瓶有利可图
if (gb < pb){
//没有。玻璃瓶
int tmp=mny-ge/gb;
litrs=tmp>0?tmp:0;
mny-=litrs*gb;
//没有。塑料瓶
litrs+=mny/pb;
cout<<"Maximum Liters of water : "<<litrs<<endl;
}
//只有塑料瓶
else
cout<<"Maximum Liters of water只有塑料瓶: "<<(mny /pb)<<endl;
}
int main(){
int money = 20, pbottle=5, gbottle=10, gempty = 8;
gbottle=gbottle-gempty; //new cost of glass botlles
maxWater( money,pbottle,gbottle,gempty );
}输出结果
Maximum Liters of water: 14