Targeting (recursion) Problems

Source: Internet
Author: User

An old question:

How many types of attacks can a Shooting athlete send with 10 shots and 90 rings? Use a program to print out each type. (Each score is an integer between 0 and 10 rings. The score can be set to 0 or 10 rings ).

 

Idea: Use recursive thinking

First, take into account that a total of ten shots are required. You can consider the following three situations:

1. If you have exceeded ten guns or the points have exceeded the given value, the system returns

2. If the current number is 10, determine whether the points of the last gun may reach the given value (0 ~ 10)

If possible, add 1 to the value and then return; otherwise, return directly.

3. Continue recursion in other cases

Source code:

# Include <iostream> <br/> using namespace STD; </P> <p> int sum = 0; <br/> int score = 90; </P> <p> void compute (INT num, int scores) <br/>{< br/> If (Num <= 0 | scores> score) return; </P> <p> If (num = 1) <br/> {<br/> If (scores + 10> = score) sum ++; <br/> return; <br/>}< br/> for (INT I = 0; I <= 10; + I) <br/> {compute (num-1, scores + I) ;}< br/>}</P> <p> int main () <br/>{< br/> compute (10, 0 ); <br/> cout <sum <Endl; <br/> return 0; <br/>}

 

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.