The daily walkthrough of the classic Algorithm--the second question five co-wells

Source: Internet
Author: User

The original: The classic algorithm daily walkthrough--the second question five home well

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?

Analysis: The same problem is also the indefinite equation, take the title of this topic is to let everyone in the indefinite equation set this category problem to achieve "extrapolate", according to test instructions

We set the well deep h, each family is a,b,c,d,e, then we can list the following equation group:

2a+b=h①

3b+c=h②

4c+d=h③

5d+e=h④

6e+a=h⑤

First we look at the idea of the ordinary youth, their idea is to find the correspondence between A,b,c,d,e .

In turn, the ② into the ①,③ generation into the ②,④ into the ③,⑤ generation into the ④ can be the following equation group:

A=b+c/2

B=c+d/3

C=d+e/4

D=e+a/5

From the computer's point of view, I do not want to have a decimal, so I can infer: C must be a multiple of 2, D must be a multiple of 3, E must be a multiple of 4, a must be a multiple of 5, according to this relationship we

You can have the following code:

1 namespaceTest2 {3     class Program4     {5         Static voidMain (string[] args)6         {7             intA, B, C, D, E, H;8 9A = b = c = d = e = h =0;Ten  One             BOOLFlag =true; A  -              while(flag) -             { the                 //multiples of 4 -E + =4; -  -A =0; +  -                  while(flag) +                 { A                     //multiples of 5 atA + =5; -  -D = e + A/5; -  -c = d + E/4; -  in                     ifC2!=0) -                         Continue; to  +                     if(d%3!=0) -                         Continue; the  *B = C + d/3; $ Panax Notoginseng                     if(b + C/2<a) -                          Break; the  +                     if(b + C/2==a) AFlag =false; the                 } +             } -  $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); -  the Console.read (); -         }Wuyi     } the}

Also our time complexity is O (N2), which is urgently needed for optimization.

Let's take a look at the idea of literary youth, whose idea is to find the correspondence between a number in A,b,c,d,e and H.

For example, I would like to find the correspondence between C and H, the above ①②③④⑤ can be written as the following groups of equations:

B=h-2a⑥

C=h-3b⑦

D=h-4c⑧

E=h-5d⑨

A=h-6e⑩

⑥,⑧,⑨,⑩ respectively into the ⑦, a spasm after the following:

C= (148/721) H

The above formula also shows the proportional relationship of C and H, we make h=721k, then c=148k, substituting it into ⑥,⑦,⑧,⑨,⑩ can be as follows equations

a=265k

b=191k

c=148k

d=129k

e=76k

x=721k

And because of k>0, so the topic has countless solutions. Here I take 0<k<5, otherwise the rope has reached the limit, need to use Dragon number to deep dive.

1 namespaceTest2 {3     class Program4     {5         Static voidMain (string[] args)6         {7              for(intK =1; K <5; k++)8             {9                 inth =721*K;Ten  One                 intA =265*K; A  -                 intb =191*K; -  the                 intc =148*K; -  -                 intD =129*K; -  +                 intE = the*K; -  +Console.WriteLine ("a={0},b={1},c={2},d={3},e={4}------h={5}\n", A, B, C, D, E, h); A             } at  - Console.read (); -         } -     } -}

I believe we will have a similar problem in the future, we should be confident.

The daily walkthrough of the classic Algorithm--the second question five co-wells

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.