Title demand: Chicken Weng A value of five, chicken mother a value of three, chickens three valuable one. Hundred money buys hundred chicken, asks the chicken Weng, the chicken mother, chickens each geometry?
The first algorithm (also the most time-consuming, by the poor lift to find out): This method can be optimized to know the number of two chickens, the third one will know
public static void Hundred_money () {for (int i = 0; I <=, i++) {for (int j = 0; J <=; J + +) {
for (int z = 0; z <=; z++) { if (i*5+j*3+z/3.0f==100&&i+j+z==100) { Console.Write ("Rooster number: { 0}, the number of hens has: {1}, the number of chickens are: {2} ", i,j,z); Console.WriteLine ();}}}}
The second algorithm: (derivation using mathematical formulas)
public void Hundred_money1 () { //number of cocks int cockcount = 0; The number of hens float hencount=0; The number of Chicks float smallcount=0; for (cockcount = 0; cockcount < cockcount++) { Hencount = 25-7f/4f * COCKCOUNT; Smallcount = + 3f/4f * cockcount; if (hencount>0&&smallcount>0) { if ((int) hencount==hencount) && ((int) smallcount= =smallcount) { Console.WriteLine ("Rooster number: {0}, number of hens: {1}, number of chickens: {2}", Cockcount,hencount,smallcount); } } } }
The third algorithm (calculated using recursion):
Class MainClass {public static void Main (string[] args) {Methodclass method = new Me Thodclass (); Int[] pricearray={15,9,1}; Int[] resultarray={0,0,0}; Method. Hundred_money2 (pricearray,resultarray,300,0); }}class methodclass{public void Hundred_money2 (int[] pricearray,int[] resultarray,int remain_money,int index) {int Time=remain_money/pricearray[index]; for (int i=0;i<=time;i++) {resultarray[index]=i; if (index==2) {if (Resultarray[0]+resultarray[1]+resultarray[2]==100&&resultarray [0]>=0&&resultarray[1]>=0&&resultarray[2]>=0) if (Resultarray[0]*pricearray [0]+resultarray[1]*pricearray[1]+resultarray[2]*pricearray[2]==300] Console.WriteLine ("Rooster has:" +r Esultarray[0]+ ", the hen has:" +resultarray[1]+ ", The chick has:" +resultarray[2]); } else {int remainmoney=remain_money; Remainmoney=remainmoney-pricearray[index]*resultarray[index]; Hundred_money2 (pricearray,resultarray,remainmoney,index+1); } } }}
The above three kinds is hundred money buys hundred chicken's three kinds of algorithm!!!!!
Hundred money buy hundred chicken three kinds of Algorithm (C # Edition)