当前位置: 代码迷 >> J2SE >> 施用 Math.random()来实现百钱百鸡方法
  详细解决方案

施用 Math.random()来实现百钱百鸡方法

热度:655   发布时间:2016-04-24 16:09:17.0
使用 Math.random()来实现百钱百鸡方法?
我换了很多种方法
不管是嵌入FOR循环   还是   if语句
都要报错。
无法使用random   来实现百钱百鸡呢?

高手麻烦写个来看看

------解决方案--------------------
up
------解决方案--------------------
什么是 百钱百鸡 呀?
小弟不是很懂,还请指教
------解决方案--------------------
关注
------解决方案--------------------
能详细介绍一下“百钱百鸡”吗?
------解决方案--------------------
/*百钱买百鸡
*公鸡五钱一只;母鸡三钱一只;小鸡一钱三只;
*
*现有一百钱,问可以买多少只公鸡,母鸡.小鸡?
*/
public class Baiqian
{
public static void main(String [] args)
{
int x=0,z=0;
int y;
for(int i=0;i <20;i++){
x=i;
for(int j=3;j <100;j+=3){
z=j;
y=100-x-z;
if(5*x+3*y+z/3==100&&y> 0&&x> 0){
System.out.print(x+ "\t "+y+ "\t "+z+ "\t ");
System.out.println();
}
}
}
}
}
这是我最初写的代码。
直接给你贴出来了。
------解决方案--------------------
public class Chicken {
private static int chicken1 = 15;
private static int chicken2 = 9;
private static int chicken3 = 1;
private static int total = 300;

/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i <= 20; i ++ ){
for (int j = 0; j <= 34; j ++ ){
for (int k = 0; k <= 300; k += 3){
if( i*chicken1 +j*chicken2+k*chicken3 == total){
System.out.println( "( " + i + ", " + j + ", " + k + ") ");
}
}
}
}
}

}

输出结果:
(0,0,300)
(0,1,291)
(0,2,282)
(0,3,273)
(0,4,264)
(0,5,255)
(0,6,246)
(0,7,237)
(0,8,228)
(0,9,219)
(0,10,210)
(0,11,201)
(0,12,192)
(0,13,183)
(0,14,174)
(0,15,165)
(0,16,156)
(0,17,147)
(0,18,138)
(0,19,129)
(0,20,120)
(0,21,111)
(0,22,102)
(0,23,93)
(0,24,84)
(0,25,75)
(0,26,66)
(0,27,57)
(0,28,48)
(0,29,39)
(0,30,30)
(0,31,21)
(0,32,12)
(0,33,3)
(1,0,285)
(1,1,276)
(1,2,267)
(1,3,258)
(1,4,249)
(1,5,240)
(1,6,231)
(1,7,222)
(1,8,213)
(1,9,204)
(1,10,195)
(1,11,186)
(1,12,177)
(1,13,168)
(1,14,159)
(1,15,150)
(1,16,141)
(1,17,132)
(1,18,123)
(1,19,114)
(1,20,105)
(1,21,96)
(1,22,87)
(1,23,78)
(1,24,69)
(1,25,60)
(1,26,51)
(1,27,42)
(1,28,33)
(1,29,24)
(1,30,15)
(1,31,6)
(2,0,270)
(2,1,261)
(2,2,252)
(2,3,243)
(2,4,234)
(2,5,225)
(2,6,216)
(2,7,207)
(2,8,198)
(2,9,189)
(2,10,180)
(2,11,171)
(2,12,162)
(2,13,153)
(2,14,144)
(2,15,135)
(2,16,126)
(2,17,117)
(2,18,108)
(2,19,99)
(2,20,90)
(2,21,81)
(2,22,72)
(2,23,63)
(2,24,54)
(2,25,45)
(2,26,36)
(2,27,27)
(2,28,18)
(2,29,9)
(2,30,0)
(3,0,255)
(3,1,246)
(3,2,237)
(3,3,228)
(3,4,219)
(3,5,210)
(3,6,201)
(3,7,192)
  相关解决方案