百钱买百鸡的Java小程序


public class HunderDollarsHundredChicken {

	/**
	 * 百钱买百鸡问题:
	 * 已知大公鸡三文钱一只,大母鸡两文钱一只,
	 * 小鸡一文钱买三只。现有100文钱,想买100只鸡,
	 * 请编写程序解决这个问题。
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int male ;
		int female ;
		int chick ;
		for(male=1;male<33;male++){
			for(female=1;female<33;female++){
				chick = 100-male-female;
				if(male*3+female+chick/3==100 && chick%3==0){
					System.out.print("You should buy Male Chicken "+ male);
					System.out.print(",Female Chicken "+female);
					System.out.println(",small Chicken "+ chick);
				}
			}
		}
	}

}



输出结果是:
You should buy Male Chicken 17,Female Chicken 32,small Chicken 51
You should buy Male Chicken 18,Female Chicken 28,small Chicken 54
You should buy Male Chicken 19,Female Chicken 24,small Chicken 57
You should buy Male Chicken 20,Female Chicken 20,small Chicken 60
You should buy Male Chicken 21,Female Chicken 16,small Chicken 63
You should buy Male Chicken 22,Female Chicken 12,small Chicken 66
You should buy Male Chicken 23,Female Chicken 8,small Chicken 69
You should buy Male Chicken 24,Female Chicken 4,small Chicken 72

你可能感兴趣的:(java)