啤酒每罐2.3元,饮料每罐1.9元。小明买了若干啤酒和饮料,一共花了82.3元。
我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒。
注意:答案是一个整数。
答案
11
解题过程
很常规的题目,需要注意这里浮点数比较采用的是:abs(实际值-比较的值)<1e-n 。
附上代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double p_price,y_price;
int x,y;
for(x=0;x<=38;x++)
{
for(y=0;y<=44;y++)
{
if(x<y)
{
p_price=x*2.3;
y_price=y*1.9;
if(abs(p_price+y_price-82.3)<1e-8)
cout<<x<<endl;
}
}
}
return 0;
}