50

java算法之百钱买百鸡问题-10962685-51CTO博客

 6 years ago
source link: http://blog.51cto.com/10972685/2067955
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

share_default.jpg

10962685 > 正文

java算法之百钱买百鸡问题

题目:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。
解: 设,公鸡x只,母鸡y只,小鸡z只。
x+y+z=100
5x+3y+1/3z=100
(0<x<20)(0<y<33)
我们采用列举法
代码如下:
public class Teststatic{
public static void main(String[] args) {
int x;
int y;
int z;
for(x=0;x<20;x++){
for(y=0;y<33;y++){
z=100-x-y;
if(5x+3y+z/3==100&&z%3==0){
System.out.println("公鸡:"+x+"母鸡:"+y+"小鸡"+z);
}
}
}
}
}
div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK