POJ3085:Greedy algorithm

This is an introductory example of greedy algorithm, which also introduces greedy algorithm.

  1. Introduction to greedy algorithm:
    (1) Greedy algorithms always make the best choice at present. That is to say, greedy algorithm is not considered from the overall optimum. The choice it makes is only a partial optimal choice in a sense.
    (2) Key properties of greedy algorithm:
    ① Greedy choice nature. Whether a problem is suitable to be solved by greedy strategy, that is, whether the problem has the nature of greedy selection, that is, the overall optimal solution of a problem can be achieved through the selection of a series of local optimal solutions, and each choice can depend on the previous choice, but not on the later choice.
    ② Optimal substructure properties. When the optimal solution of a problem contains the optimal solution of its subproblem, it is said that the problem has the property of optimal substructure. The optimal substructure property of the problem is the key to solve the problem by greedy method.
    (3) Greedy algorithm generally follows the following steps:
    ① Establish a mathematical model to describe the problem.
    ② The problem is divided into several subproblems.
    ③ Each subproblem is solved to obtain the local optimal solution of the subproblem.
    ④ The local optimal solution of the subproblem is synthesized into a solution of the original solution problem.
    (4) Difference between greedy algorithm and dynamic programming (DP):
    Greedy selection property means that the global optimal solution of the problem can be achieved through a series of local optimal choices, namely greedy selection. This is also the main difference between greedy algorithm and dynamic programming algorithm.
    The dynamic programming algorithm usually solves each sub problem in a bottom-up way, while the greedy algorithm is usually carried out in a top-down way and makes successive greedy choices in an iterative way. Each greedy choice simplifies the problem into a smaller sub problem.
  2. Problem Overview:
    Given is the total amount in cents. There are four kinds of coins: quarter, dime, nickel and penny. It is required to give the minimum combination of coins.
  3. Strategy for solving the problem:
    (1) The larger the score, the more coins (such as quarter), the less the total number of coins;
    (2) The strategy is: first select the coin with large score, and select the four scores in turn. Therefore, greed is always selected in the order of score from large to small.
    (3) The subproblem is: after matching the quantity according to the maximum quantity, the remaining amount is a subproblem, which is allocated in dime, nickel and penny.
  1. Self written code
    #include
    using namespace std;
    int main()
    {
    int t,count=0;
    cin>>t;
    while(t–)
    {
    count++;
    int n;
    cin>>n;
    int a=25,b=10,c=5,d=1;
    int a1,a2,a3,a4;
    a1 = 0;
    a2 = 0;
    a3 = 0;
    a4 = 0;
    if(n>=a)
    {
    a1=n/a;
    n=n%a;
    }
    if(n>=b)
    {
    a2=n/b;
    n=n%b;
    }
    if(n>=c)
    {
    a3=n/c;
    n=n%c;
    }
    if(n>=d)
    {
    a4=n/d;
    }
    cout<<count<<" “<<a1<<” “<<“QUARTER(S), “<<a2<<” DIME(S), “<<a3<<” NICKEL(S), “<<a4<<” PENNY(S)”<<endl;
    // cout<<count<<” “<<a1<<” “<<“QUARTER(S),”<<a2<<” DIME(S),"<<a3<<" NICKLE(S),"<<a4<<" PENNY(S)"<<endl;
    }
    system(“pause”);
    return 0;
    }

Link to original problem:
http://poj.org/searchproblem?field=source&key=

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值