A _c# tutorial on the shooting algorithm of C # recursive algorithm

Source: Internet
Author: User

question: A design athlete target, a total of 10 rings, even open 10 ring hit 90 ring the possibility of how many? Please use the recursive algorithm to achieve?

Analysis:

1 What are the possible scoring ranges for each target?
The target has 10 rings, then when hit, the score can be 1-10, if not hit the score of 0, so each target shooting range of 0-10, a total of 11 May
2 Calculate how many of the most direct methods are possible:

Target 10 times, record the 10 shooting process, and use the loop to complete

for (int i1=0;i1<=10;i++)
{for
  (int i2=0;i2<=10;i2++)
  {for (int
   i3=0;i3<=10;i3++)
   {
     ---for
     (int i10=0;i10<=10;i10++)
     {
       if (i1+i2+i3+......+i10=90)
       {
         //one possible
       }
     }
     ---
   }
  }

But there are two deficiencies in doing so:

1 if the title changed to 1000 shots, the probability of scoring 900, it is estimated that this type of writing to cry
2 inadequate consideration, if the first shooting is divided into 0, there are 9 opportunities, this 9 times, the demand for guns are full marks, if the second shot, the score is not 10, then the third gun can not fight to know that there may be no possibility. Just like the table tennis game, 5 innings 3 wins, if the 3 innings are a winner, then the game can be concluded at this time. And going on is a waste of time and energy.

To solve the above problems by using the method of recursion

The first is to adjust themselves, if there is no end limit, the effect of the return and dead loop is the same, but the normal situation will have the end of the symbol, and the meaning of the return is to complete the loop layer is not clear or the number of clear layers but the number is very large situation. The point of using it is that the normalized function must have one or more parameters, and the dead loop is formed without the first return of the parameter. And the function of the first call itself each time, need to carefully control the parameters. As far as possible to prevent the production of dead loops, the first and stack relationship closely.

To achieve the above functions, the function of the function to complete the main:

1 when the current target number is less than 1, or greater than the specified number of times, you should exit the execution of the return function
2 when the remaining target number of shooting every time get full score, but can not achieve the goal score, should quit the first return
3 If neither of the above is the case, the return should be performed

Implementation code:

 using System;
 namespace Test {///<summary>///Shotscore's summary description.
  </summary> public class Shotscore {//total number of possibilities int sumrate = 0;
  Probability range of each possible hit int[] Scorearray;
  How many points int totalscore=0 is needed in total;
  How many times can I hit int totalshot=0; Number of current hit rings public Shotscore (int[] sa,int ts,int t) {this.
   Scorearray = sa;
   This.totalshot = ts;
  This.totalscore = t;
  public int getsum () {return sumrate; public void Compute (int currentshot,int cnum) {//hit more dozen less than not. if (currentshot<0| |
   Currentshot>totalshot) {return;
   The gun will not be able to meet the conditions after 10, game over if ((totalshot-currentshot+1) *10) < (Totalscore-cnum)) {return; 
    //Hit enough times and reached the expected number of rings if (currentshot==totalshot) {//the possibility of establishing sumrate++; 
   Return
   for (int i=0;i<scorearray.length;i++) {Compute (currentshot+1,cnum+scorearray[i]); }
   
  }
 }
}

The final results are: 92378
Summary: This question mainly examines the programmer's logical thinking ability and the application to the first function. Very simple. But the logic must be clear, the method of analyzing the problem must be accurate.

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.