Coin ChangeAC代码递归+详细的注释

Coin Change

 

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21645    Accepted Submission(s): 7577

 

 

 

Problem Description

 

 

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.
 

 

Input

 

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
 

 

Output

 

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
 

 

Sample Input

 

 
11 26
 

 

Sample Output

 

 
4 13

 

 

 
代码及注释:
//附上详细的注释只是想让在看的你更加容易理解,不需要去句句斟酌浪费时间;
//写的很仓促,有写的不好的地方还请包涵谢谢
#include<iostream>
using namespace std;
int coin[]={50,25,10,5,1};
int countt;//累计种数
int account=0;//输入金币
void change(int num,int j,int coinnum)
{
    if(num==account&&coinnum<=100)//累计和num等于输入金额&&硬币个数小于等于100个,计数
        countt++;
    else if(num>account||coinnum>100)//累计和num大于输入金额||硬币个数大于100个,结束
        return;
    else//累计和num小于输入金额,继续递归
    for(int i=j;i<5;i++)
    {
        j=i;coinnum+=1;//这里加了个j是因为coin[0-->j]面值的硬币在前面已经算过了,比如:account=50;第一次递归:j=i=0;coinnum=0+1;
        change(num+coin[i],j,coinnum);//change(0+50,0,1);第二次递归开始:if(num==account&&coinnum<=100) 满足,计数,结束
        coinnum-=1;                   //coinnum==coinnum-1==1-1==0;i++,i=1;j=1,即去掉50;coinnum=1;change(25,1,1)第三次递归开始:
    }                                 //25小于50,继续递归;i=j=1(设置j用以就在此,不然如果i从0开始会重复计数)j=1;coinnum=1+1=2;
                                    //chang(25+25,1,2);第四次递归开始:if(num==account&&coinnum<=100) 满足,计数,结束
                                    //*
                                    //*
                                    //*
}                                   //如果没有j,像(25,10,10,5),(10,25,10,5},{5,25,10,10}会重复
int main()
{
    account=0;
    while(cin>>account)
       {
        countt=0;
        change(0,0,0);
        cout<<countt<<endl;
        account++;
       }
    return 0;
}
觉得有用的话就赞一个吧亲
ps:第一次在csdn上写文章
以下是一个简单的Verilog自动售货机代码,附有详细注释,以便您理解每个模块的功能: ``` // 定义模块名称和输入输出端口 module VendingMachine( input clk, // 时钟信号 input rst, // 复位信号 input coin, // 硬币输入信号 input note, // 纸币输入信号 input select, // 商品选择信号 input buy, // 购买信号 output reg[3:0] price, // 商品价格输出端口 output reg[1:0] goods // 商品选择输出端口 ); // 定义状态机的状态 parameter IDLE = 2'b00; parameter SELECT = 2'b01; parameter PAY = 2'b10; parameter DELIVER = 2'b11; // 定义状态机的当前状态和下一个状态 reg[1:0] state, next_state; // 定义商品价格和选择 reg[3:0] coke_price = 4'b0100; // 可乐价格为4个硬币 reg[3:0] sprite_price = 4'b1000; // 雪碧价格为8个硬币 reg[1:0] selected_goods; // 选择的商品 // 定义计数器,用于统计硬币和纸币数量 reg[3:0] coin_count = 4'b0000; reg[3:0] note_count = 4'b0000; // 定义状态机的状态转移逻辑 always @ (posedge clk) begin if (rst) begin // 复位信号 state <= IDLE; // 进入待机状态 selected_goods <= 2'b00; // 未选择商品 coin_count <= 4'b0000; // 硬币数量清零 note_count <= 4'b0000; // 纸币数量清零 end else begin state <= next_state; // 状态转移到下一个状态 end end // 定义状态机的输出逻辑 always @ (*) begin case (state) IDLE: begin // 待机状态 price <= 4'b0000; // 商品价格为0 goods <= 2'b00; // 商品选择为0 if (select) begin // 等待用户选择商品 next_state <= SELECT; // 转移到选择状态 end else begin next_state <= IDLE; // 等待用户选择 end end SELECT: begin // 选择状态 if (selected_goods == 2'b00) begin // 如果还未选择商品 if (select == 2'b01) begin // 选择可乐 selected_goods <= 2'b01; // 设置商品为可乐 price <= coke_price; // 设置价格为可乐价格 end else if (select == 2'b10) begin // 选择雪碧 selected_goods <= 2'b10; // 设置商品为雪碧 price <= sprite_price; // 设置价格为雪碧价格 end end else begin // 如果已经选择商品 if (buy) begin // 如果用户选择购买商品 next_state <= PAY; // 转移到付款状态 end else begin next_state <= SELECT; // 等待用户购买商品 end end end PAY: begin // 付款状态 if (coin) begin // 如果用户投入硬币 coin_count <= coin_count + 1; // 硬币数量加一 if (coin_count >= price) begin // 如果硬币数量足够 next_state <= DELIVER; // 转移到交付商品状态 end end else if (note) begin // 如果用户投入纸币 note_count <= note_count + 1; // 纸币数量加一 if (note_count >= price) begin // 如果纸币数量足够 next_state <= DELIVER; // 转移到交付商品状态 end end else begin next_state <= PAY; // 等待用户付款 end end DELIVER: begin // 交付商品状态 goods <= selected_goods; // 输出选择的商品 if (selected_goods == 2'b01) begin // 如果选择的是可乐 coin_count <= coin_count - coke_price; // 扣除硬币数量 end else if (selected_goods == 2'b10) begin // 如果选择的是雪碧 coin_count <= coin_count - sprite_price; // 扣除硬币数量 end next_state <= IDLE; // 转移到待机状态,等待下一次交易 end endcase end endmodule ``` 以上代码中,定义了一个自动售货机模块,包括时钟信号、复位信号、硬币输入信号、纸币输入信号、商品选择信号、购买信号和商品价格输出端口、商品选择输出端口等。通过状态转移逻辑和计数器实现了自动售货机的基本功能,包括选择商品、付款和交付商品等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值