Java Algorithm---Five co-wells

Source: Internet
Author: User

The ancient mathematics masterpiece "Nine chapters Arithmetic" has such a problem called "five family Well, a second administrative (twice shy) insufficient, such as (connected) b a administrative; b three administrative insufficient, such as the third administrative;

Administrative deficiency, such as Ding, butyl five administrative insufficient, such as e-Administrative, e Six administrative insufficient, such as a administrative, are all.

It means that five family share a well, a family of rope with two not enough, but also to use the rope of B to hit the water well; b The rope with three not enough, but also to use the third family of rope

One can hit the well water; the rope of the third family with four not enough, but also to use Dinggu rope one to hit the well, Dinggu rope with five not enough, but also to use the rope of the family to play

To the well, the rope of the family of six is not enough, but also to use a family of a rope in order to hit the wells.

Last question: How deep is the well? How long are the ropes per family?

Algorithm analysis: According to the problem can be listed in the equation (len for well depth)

Len1*2+len2 = Len

Len2*3+len3 = Len

LEN3*4+LEN4 = Len

Len4*5+len5 = Len

Len5*6+len1 = Len

Further simplification

Len1=len2+len3/2

Len2=len3+len4/3

Len3=len4+len5/4

Len4=len5+len1/5

From the above calculation can be obtained LEN3 is a multiple of 2, LEN4 is a multiple of 3, LEN5 is a multiple of 4, LEN1 is a multiple of 5.

Code implementation:

 Packagepractice; Public classwjgj{Static voidWjgj (int[] len1,int[] Len2,int[] Len3,int[] Len4,int[] Len5,int[] len)//Five common well algorithms    {         for(Len5[0] = 4;; len5[0] + = 4)            //Len5 is a multiple of 4             for(Len1[0] = 5;; len1[0] + = 5)//Len1 is a multiple of 5{len4[0] = Len5[0] + len1[0]/5; len3[0] = Len4[0] + len5[0]/4; if(Len3[0]% 2! = 0 | | len4[0]% 3! = 0)                    Continue;//if it cannot be divisible by 2 or if it cannot be divisible by 3, the next cyclelen2[0] = Len3[0] + len4[0]/3; if(Len2[0] + len3[0]/2 < len1[0])                     Break;//cut back to len5[0] loop (because X is too big)                if(Len2[0] + len3[0]/2 = = Len1[0]) {len[0] = 2 * (len1[0]) + (len2[0]);//Calculating Well depth                    return; }            }    }     Public Static voidMain (string[] args) {int[] Len1 = {0 }; int[] Len2 = {0 }; int[] Len3 = {0 }; int[] Len4 = {0 }; int[] Len5 = {0 }; int[] len = {0 }; Wjgj (Len1, Len2, Len3, Len4, Len5, Len); //Solution AlgorithmSystem.out.printf ("The results of the five common well problem solution are as follows: \ n");//Output ResultsSystem.out.printf ("Jia twice Shy length:%d\n", len1[0]); System.out.printf ("B home twice shy length:%d\n", len2[0]); System.out.printf ("Home twice Shy length:%d\n", len3[0]); System.out.printf ("Ding Jia Twice Shy length:%d\n", len4[0]); System.out.printf ("Xu Jia twice shy length:%d\n", len5[0]); System.out.printf ("Well depth:%d\n", len[0]); }}

C can also be used to achieve

The code is as follows

namespacetest{classProgram {Static voidMain (string[] args) {            intA, B, C, D, E, H; A= b = c = d = e = h =0; BOOLFlag =true;  while(flag) {//multiples of 4E + =4; A=0;  while(flag) {//multiples of 5A + =5; D= e + A/5; C= d + E/4; ifC2!=0)                        Continue; if(d%3!=0)                        Continue; b= C + D/3; if(b + C/2<a) Break; if(b + C/2==a) flag=false; }} H=2* A +b; Console.WriteLine ("a={0},b={1},c={2},d={3},e={4}------h={5}\n", A, B, C, D, E, h);        Console.read (); }    }}

Java Algorithm---Five co-wells

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.