22 lines
737 B
Java
22 lines
737 B
Java
package LanQiaoOJ;
|
||
/*啤酒与饮料
|
||
* 题目描述
|
||
啤酒每罐 2.3元,饮料每罐 1.9元。小明买了若干啤酒和饮料,一共花了 82.3元。
|
||
我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒。*/
|
||
public class 啤酒与饮料 {
|
||
public static void main(String[] args) {
|
||
int x,y;//分别表示啤酒与饮料的数量
|
||
for(x=1;x<35;x++)
|
||
for(y=1;y<43;y++) {
|
||
if(x<y&&2.3*x+1.9*y==82.3) {
|
||
System.out.println(x);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/*
|
||
* 总结:
|
||
* 在做本题时,首先想到的是想用数学方法解决,建立等式,但是,在本题中,
|
||
* 如果采用一般的数学思维,很有可能做不出来,因为从题干中只能得到一个等式和一个不等式,无法解决。
|
||
* 此题从编程角度看,实际上非常简单,两个嵌套循环遍历,再用一个if语句判断,便可直接得出结果。
|
||
*/ |