Q: How likely is a design athlete to hit a target with a total of 10 rings and even a 10 ring to hit a 90 ring? Use the default Algorithm Implementation?
Analysis:
1) What is the possible score range for each target?
If the target has 10 rings, the score can be 1-10 when the target is hit. If the target is not hit, the score is divided into 0. Therefore, the range of each target score is 0-10, which may be 11 in total.
2) the number of possible and most direct calculation methods:
Hit 10 targets, record these 10 targets respectively, and use loops 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 possibility
}
}
---
}
}
}
However, there are two shortcomings:
1) if the question is changed to the possibility of a 1000-gun connection and a score of 900, it is estimated that this writing will start to cry.
2) If the score for the first shot is 0 and there are still 9 chances, the full score is required for the 9 chances. If the second shot is not 10, the third shot may be impossible if you don't have to fight. For example, in a table tennis competition, if the team wins in the 5-game and 3-Game competitions, the competition will end at this time. Continuing is a waste of time and energy.
2. Resolve the above problems using the default method
If there is no end limit, the effect of the first entry is the same as that of the dead loop. However, under normal circumstances, there will be an end sign, in addition, the meaning of the regression lies in the case that the number of completed cycles is unclear or the number of layers is clear but the number is very large. Note that the regression function must have one or more parameters. an endless loop is formed when no parameters exist. In addition, every time a function calls itself, you need to carefully control parameters. Avoid endless loops as much as possible. The first rule is closely related to the stack.
To implement the above functions, the functions to be completed are:
1) when the number of input targets is less than 1 or greater than the specified number, exit the execution of the homing function.
2) When the full score is obtained for the remaining number of targets, but the target score cannot be reached, you should exit
3) if neither of the preceding conditions exists
Implementation Code :
1 Using System;
2
3 Namespace Test
4 {
5 /**/ /// <Summary>
6///Summary of shotscore.
7/// </Summary>
8 Public Class Shotscore
9 {
10 // Total possibilities
11 Int Sumrate = 0 ;
12 // Probability range of each possible hit
13 Int [] Scorearray;
14 // Total points required
15 Int Totalscore = 0 ;
16 // Total number of hits
17 Int Totalshot = 0 ;
18 // Currently, the total number of central hits
19 Public Shotscore ( Int [] SA, Int TS, Int T)
20 {
21 This . Scorearray = SA;
22 This . Totalshot = TS;
23 This . Totalscore = T;
24 }
25 Public Int Getsum ()
26 {
27ReturnSumrate;
28}
29 Public Void Compute ( Int Currentshot, Int Cnum)
30 {
31 // It won't work if you play more or less.
32 If (Currentshot < 0 | Currentshot > Totalshot)
33 {
34Return;
35}
36 // In the future, none of the 10 guns will meet the conditions. Game over
37 If (Totalshot - Currentshot + 1 ) * 10 ) < (Totalscore - Cnum ))
38 {
39Return;
40}
41 // The number of hits reached the expected number of loops.
42 If (Currentshot = Totalshot)
43 {
44 // This possibility is true
45 Sumrate ++ ;
46 Return ;
47 }
48 For ( Int I = 0 ; I < Scorearray. length; I ++ )
49 {
50Compute (currentshot+1, Cnum+Scorearray [I]);
51}
52
53 }
54 }
55 }
56
The final result is: 92378.
Conclusion: This problem mainly involvesProgramThe logical thinking ability of the member and the application of the nth function. Very simple. However, the logic must be clear and the problem analysis methods must be accurate.