中國古代數學家張丘建在他的《算經》中提出了一個著名的“百錢買百雞問題”:一隻公雞值五錢,一隻母雞值三錢,三隻小雞值一錢,現在要用百錢買百雞,請問公雞、母雞、小雞各多少只? 1 package program1; 2 //百錢買百雞:一隻公雞五錢,一隻母雞三錢,三隻小雞一錢 3 //公雞:cock,母 ...
中國古代數學家張丘建在他的《算經》中提出了一個著名的“百錢買百雞問題”:一隻公雞值五錢,一隻母雞值三錢,三隻小雞值一錢,現在要用百錢買百雞,請問公雞、母雞、小雞各多少只?
1 package program1; 2 //百錢買百雞:一隻公雞五錢,一隻母雞三錢,三隻小雞一錢 3 //公雞:cock,母雞:hen,小雞:chicken 4 public class ch01 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int cock; 9 int hen; 10 int chicken; 11 for( cock = 0 ; cock <= 20 ; cock ++ ) 12 { 13 for( hen = 0 ; hen < 33 ; hen ++ ) 14 { 15 for(chicken = 0 ; chicken <= 100 ; chicken ++ ) 16 { 17 // if((coke + hen + chicken == 100) && (5 * coke + 3 * hen + chicken / 3 == 100)) 18 //犯錯1;chicken / 3 是對3取整,類型不對,應為小數 19 if((cock + hen + chicken == 100) && (5 * cock + 3 * hen + chicken / 3.0 == 100)) 20 { 21 System.out.println("母雞:"+ cock + "公雞:" + hen + "小雞:" + chicken); 22 } 23 } 24 } 25 } 26 }
1 package program1; 2 //百錢買百雞:一隻公雞五錢,一隻母雞三錢,三隻小雞一錢 3 //公雞:cock,母雞:hen,小雞:chicken 4 public class ch011 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 int cock ; 9 int hen ; 10 for( cock = 0 ; cock <= 20 ; cock ++ ) 11 { 12 for( hen = 0 ; hen < 33 ; hen ++ ) 13 { 14 int chicken = 100 - cock - hen; 15 if(5 * cock + 3 * hen + chicken / 3.0 == 100) 16 { 17 System.out.println("母雞:"+ cock + "公雞:" + hen + "小雞:" + chicken); 18 } 19 20 } 21 } 22 } 23 24 }View Code