算法练习01: 百钱买百鸡

package com.kay.day01;

import org.junit.Test;

/**
*
* <p>Title: Run</p>
* <p>Description: 算法练习01:百钱买百鸡
* 百钱买百鸡的问题算是一套非常经典的不定方程的问题,题目很简单:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
*用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。</p>
* @author kay
*/
public class Run {
/**
* 如果把公鸡为x,母鸡为y,小鸡为z,则可以得到方程
* x + y + z = 100
* 5x + 3y + z/3 = 100
*/
public void Test(){
for (int x = 1; x < 20; x++){
for (int y = 1; y < 33; y++){
int z = 100 - x - y;

if ((z % 3 == 0)&&(5 * x + 3 * y + z / 3 == 100)){
System.out.println("公鸡:" + x + "," + "母鸡:" + y + "," + "小鸡:" + z);
}
}
}
}

/**
* 如果用数学方法进行推算,就可以得到
* x = 4k;
* y = 25 - 7k;
* z = 75 + 3 * k;
*/
@Test
public void Test1(){
int x = 0,y = 0,z = 0;

for (int k = 1; k < 4;k++){
x = 4 * k;
y = 25 - 7 * k;
z = 75 + 3 * k;

System.out.println("公鸡:" + x + "," + "母鸡:" + y + "," + "小鸡:" + z);
}
}
}


--来自网页客户端
4.jpg
已邀请:

要回复问题请先登录注册

Template error, template file not found