Table of Contents
Example
Algorithm
Output
Home Backend Development C++ C/C++ program for greedy algorithm to find the minimum number of coins

C/C++ program for greedy algorithm to find the minimum number of coins

Sep 19, 2023 pm 11:01 PM
greedy algorithm c/c Minimum number of coins

C/C++ program for greedy algorithm to find the minimum number of coins

The greedy algorithm is an algorithm used to find the optimal solution to a given problem. The greedy algorithm works by finding a local optimal solution for each part (the optimal solution to one part of the problem), thus showing that a global optimal solution can be found.

In this problem we will use the Greedy Algorithm algorithm to find the minimum number of coins/notes that can make up a given sum. For this we will consider all valid coins or banknotes, i.e. denominations { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}. We need to return the number of coins/notes needed to make up the sum.

Let us give a few examples to understand the context better -

Example 1 -

Input : 1231
Output : 7
Copy after login

Instructions - We need two 500 rupee notes , two 100 rupee notes, one 20 rupee note, one 10 rupee note and one Re 1 coin. The total is 2 2 1 1 1 = 7

Example 2 -

Input : 2150
Output : 3
Copy after login

Instructions - We need one Rs 2000 note, one Rs 100 note and one Rs 50 note.

To solve this problem using a greedy algorithm, we will find the largest denomination of banknotes that can be used. We will then subtract the maximum denomination from the sum and do the same process again until the sum is zero.

Algorithm

Input: sum,
Initialise the coins = 0
Step 1: Find the largest denomination that can be used i.e. smaller than sum.
Step 2: Add denomination two coins and subtract it from the Sum
Step 3: Repeat step 2 until the sum becomes 0.
Step 4: Print each value in coins.
Copy after login

Example

Real-time demonstration

#include <bits/stdc++.h>
using namespace std;
int notes[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 };
int n = sizeof(notes) / sizeof(notes[0]);
void minchange(int sum){
   vector<int> coins;
   for (int i = n - 1; i >= 0; i--) {
      while (sum >= notes[i]) {
         sum -= notes[i];
         coins.push_back(notes[i]);
      }
   }
   for (int i = 0; i < coins.size(); i++)
      cout << coins[i] << "\t";
}
int main(){
   int n = 3253;
   cout << "The minimum number of coins/notes that sum up " << n << " is \t ";
   minchange(n);
   return 0;
}
Copy after login

Output

The minimum number of coins/notes that sum up 3253 is
2000 500 500 200 50 2 1
Copy after login

The above is the detailed content of C/C++ program for greedy algorithm to find the minimum number of coins. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement greedy algorithm in C# How to implement greedy algorithm in C# Sep 19, 2023 am 11:48 AM

How to implement the greedy algorithm in C# The greedy algorithm (Greedy algorithm) is a commonly used problem-solving method. It selects the current optimal solution every time in the hope of obtaining the global optimal solution. In C#, we can use greedy algorithms to solve many practical problems. This article will introduce how to implement the greedy algorithm in C# and provide specific code examples. 1. Basic principles of greedy algorithm The basic idea of ​​greedy algorithm is to choose the current optimal solution every time, regardless of the possible impact of subsequent steps. This kind of thinking

In C/C++, the strcmp() function is used to compare two strings In C/C++, the strcmp() function is used to compare two strings Sep 10, 2023 am 11:41 AM

Thefunctionstrcmp()isabuilt-inlibraryfunctionanditisdeclaredin“string.h”headerfile.Thisfunctionisusedtocomparethestringarguments.Itcomparesstringslexicographicallywhichmeansitcomparesboththestringscharacterbycharacter.Itstartscomp

How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? Sep 19, 2023 am 10:22 AM

How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? Introduction: In daily life, we often need to make change, especially when shopping or trading. To use as few coins as possible, the change amount should be combined using as few coins as possible. In computer programming, we can use a greedy algorithm to solve this problem to get an efficient solution. This article will introduce how to use the greedy algorithm in PHP to achieve an efficient solution to the minimum coin change problem, and provide corresponding code examples.

Parse the Ford-Fulkerson algorithm and implement it through Python Parse the Ford-Fulkerson algorithm and implement it through Python Jan 22, 2024 pm 08:09 PM

The Ford-Fulkerson algorithm is a greedy algorithm used to calculate the maximum flow rate in a network. The principle is to find an augmenting path with a positive remaining capacity. As long as the augmenting path is found, you can continue to add paths and calculate traffic. Until the augmenting path no longer exists, the maximum flow rate can be obtained. The term remaining capacity of the Ford-Fulkerson algorithm is to subtract the flow from the capacity. In the Ford-Fulkerson algorithm, the remaining capacity is a positive number before it can continue to be used as a path. Residual network: It is a network with the same vertices and edges, using residual capacity as capacity. Augmented path: It is the path from the source point to the receiving point in the residual graph, with a final capacity of 0. A possible outline of the Ford-Fulkerson algorithm principle example

In C/C++, the fseek() function is used to move the position of the file pointer in the file. In C/C++, the fseek() function is used to move the position of the file pointer in the file. Sep 02, 2023 pm 03:57 PM

fseek() is used in C language to move the file pointer to a specific location. Offsets and streams are the targets of pointers, and they are given in function arguments. If successful, it returns zero. If unsuccessful, it returns a non-zero value. The following is the syntax of fseek() in C language: intfseek(FILE*stream,longintoffset,intwhence) Here are the parameters used in fseek(): stream− This is the pointer used to identify the stream. offset − This is the number of bytes from the position. whence−This is where the offset is added. whence is given by the following constants

How to implement greedy algorithm using Python? How to implement greedy algorithm using Python? Sep 19, 2023 am 11:43 AM

How to implement greedy algorithm using Python? Greedy Algorithm is a simple and effective algorithm suitable for solving problems with optimal substructure properties. It takes the best choice in the current state in each step of selection, hoping to find the global optimal solution. In this article, we will introduce how to use Python to implement the greedy algorithm, with specific code examples. 1. The basic idea of ​​the greedy algorithm The basic idea of ​​the greedy algorithm is to select the optimal solution in the current state at each step, and then

How to write a greedy algorithm using PHP How to write a greedy algorithm using PHP Jul 07, 2023 pm 03:45 PM

How to use PHP to write a greedy algorithm Greedy algorithm (Greedy algorithm) is a simple and effective algorithm used to solve a type of optimization problem. Its basic idea is to make the choice at each step that seems best at the moment, without regard to future consequences. This article will introduce how to write a greedy algorithm using PHP and provide relevant code examples. 1. Problem Description Before explaining the greedy algorithm, let us first define a specific problem for a better understanding. Suppose there is a set of tasks, each task has a start

Greedy algorithm and its implementation in C++ Greedy algorithm and its implementation in C++ Aug 22, 2023 am 10:04 AM

The greedy algorithm is a commonly used algorithm idea and is widely used in many problems. The core idea is to only consider the immediate optimal solution when making a decision at each step, without considering the long-term impact. In C++, the implementation of greedy algorithms often involves basic operations such as sorting and data processing. Below, we will introduce the idea of ​​greedy algorithm and its implementation in C++ for several typical problems. 1. Activity Scheduling Problem Given a set of activities, each activity has its start time and end time, and a person can only participate in one activity at a time.

See all articles