百錢買百雞的問題算是一套非常經典的不定方程的問題,題目很簡單:公雞5文錢一只,母雞3文錢一只,小雞3只一文錢, 用100文錢買一百只雞,其中公雞,母雞,小雞都必須要有,問公雞,母雞,小雞要買多少只剛好湊足100文錢。
public class TestChicken{ //O(n^2) public static void Demo1(){ for(int x = 1 ; x < 20 ; x++){ for(int y = 1 ; y < 33 ; y ++){ int size = 100 - x - y ; if((size%3==0) && (5*x + 3*y + size/3 == 100) ){ System.out.參考:http://www.cnblogs.com/huangxincheng/archive/2012/08/05/2624156.html新聞熱點
疑難解答