Huawei apply for test questions-Find change (recursive solution)

Source: Internet
Author: User

input: output:
Get change.

We know that the renminbi has 1, 2, 5, 10, 20, 50, 100 of these denominations. Now give you n (1≤n≤250) yuan, let you calculate for

three ways to express 。

Topic Category: Cycle
Difficulty: Primary
Scores:
Run time limit: Ten Sec
Memory Limit: MByte
Stage: Recruitment management

input has multiple groups, Each group of rows, for a consolidation N. The input ends with 0.

There are several representations of how to output this denomination.

Sample output:
13

There are only 7 denominations of change: 1,2,5,10,20,50,100; When you enter an n value between two denominations, such as n=25, then n is between 20 and 50, then the maximum value for the change is 20; using recursive thinking, the number of ways to change the money can be divided into two categories, a class of which contains 20 denominations, The other is not included 20, the use of the addition principle, the two categories can be added, and for these two classes may also continue to divide, for the inclusion of 20, then the face value at this time becomes n-20=5, for not including 20, then the face value of the maximum is 10, so recursion down;

The recursive type is: Find (n,i) =find (n-rmb[i],i) +find (n,i-1);

Where Rmb[]={1,2,5,10,20,50,100};,i is the subscript that points to the first rmb[i with a value less than or equal to n].


#include <iostream>using namespace std;/*  */static int rmb[]={1,2,5,10,20,50,100};int find (int n,int i) {if (n <=1 | | Rmb[i]==1) return 1;return find (n-rmb[i],i) +find (n,i-1);} int main () {int n;int i;while (cin>>n && n!=0) {for (i=6;i>=0 && n<rmb[i];i--);cout<< Find (N,i) <<endl;} return 0;}




Huawei apply for test questions-Find change (recursive solution)

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.