Dog algorithm (minimum number) to find a change algorithm

xiaoxiao2021-03-06  62

Using system.collections.gener; using system.linq; using system.text;

Namespace IMPLEMTATIONS {///

/// Calculates the minimum count of change. /// public class moneychanger {/// /// initializes an instance of MoneyChange with par values. /// /// PAR VALUES (EG 10, 5, 2, 1 for RMB). public Moneychanger (int [] Parvalues) {IF (Parvalues ​​== Null) {throw new argumentnullexception ("parvalues");}

This.Parvalues ​​= this.filterparvalues ​​(PARVALUES);

IF (this.parvalues.count == 0) {Throw new ArgumentException ("Invalid Par Values");}}

///

/// gets change with minimum count of par values. /// /// Total Amount to be change. /// < Returns> (PARVALUE, Count) Pairs. Public Virtual Dictionary change (int) {dictionary result = new dictionary (this.parvalues.count);

Foreach (int parin this.parvalues) {result.add (par, amount / par); amount% = par;}

Return Result;}

#Region Fields

///

/// stores par values. /// private list parse;

#ENDREGION FIELDS

#Region Supports Methods

///

///filters par valuees with folload: /// 1. All values ​​are greater dam zero. /// 2. All Have Different value. /// 3. sorted in descending Order. // / /// Array of par values. /// filter par values. private list Filterparvalues ​​(int [] PARVALUES ) {List temp = new list ; list list = new list (PARVALUES.LENGTH); // Filters Positive Integers. Foreach (int par in parvalues) { IF (PAR> 0) {TEMP.ADD (PAR);}

// sorts values. Temp.sort ();

// Adds to list in decending Order. For (INT i = Temp.count - 1; i> = 0; I -) {= ing (list.count> 0) {// ingores the duPlicate value. If (Temp [ I] == List [list.count - 1]) {Continue;}}

List.add (Temp [i]);

Return List;}

#ndregion supports methods}}

转载请注明原文地址:https://www.9cbs.com/read-84375.html

New Post(0)