Title:There are N (n=3) root wood, the length of which is a,b,c (a=8.0,b=10.0,c=100.0), divided into the same length of X (x=100) a single copy. The maximum length of a single part.Analysis:N-Wood, etc. is divided into X-parts. The optimal solution is AVG = (a+b+c)/x. In fact, each wood may have a surplus, so that it will not be X-parts. If you don't get x, then push back. COUNT = a/avg + b/avg + c/avg. The division calculates the downward rounding. X-n < COUNT <=x。 Reason is(Ma*avg+da) + (MB*AVG+DB) + (MC*AVG+DC) = X*avg。 (A with AVG length divided into ma parts, residual da, analogy.) )0 <=da <avg。first determine the value of the MA+MB+MC , if less than X, A is divided into (ma+1) copies, that is La1 = A (ma+1). divided into (ma+2) parts,La2 = A/(ma+2). until divided into (ma+n-1) parts,Lax = A/(ma+n-1) . And so on B is divided into megabytes ... will La1, La2 ... LB1, Lb2,... In order of magnitude to small, the value of (a/l + b/l + c/l) is calculated, and if it is greater than or equal to X, L is the request. Program:
Private Static BOOLDevideincommon (Double[] Arrlength,intintcount) { //Judging there are negative numbers or 0 if(Arrlength.any (i = i <=0)) { return false; } intIntnum =arrlength.length; DoubleMaxavg = Arrlength.sum ()/intcount; int[] Arrcount = arrlength.select (i = Convert.ToInt32 (Math.floor (I/maxavg)). ToArray (); if(Arrcount.sum () <intcount) {List<Double> lstlength =Newlist<Double>(); for(inti =0; i < arrlength.length; i++) { for(intj =1; J < Intnum; J + +) { DoubleTmpL = Arrlength[i]/(Arrcount[i] +j); int[] ArrCount2 = arrlength.select (M = Convert.ToInt32 (Math.floor (M/TmpL)). ToArray (); if(Arrcount2.sum () <intcount) { Continue; } Else{lstlength.add (TmpL); Break; } } } DoubleIntresult =Lstlength.max (); Display (Arrlength, intresult); } Else{Display (arrlength, MAXAVG); } return true;}Devideincommon
Secondary display
Private Static voidDisplay (Double[] Arrlength,Doubleavglength) {Console.WriteLine ("AVG:"+avglength); foreach(DoubleLenincharrlength) {Console.WriteLine ("Length:"+ len +"Count:"+ Math.floor (len/avglength)); }}Display
Hope to be of help to you.
Algorithm: Wood