C # 2 Array Split Amount Question

My question is: I have a certain amount of money, say 552. I want to break it down in my coins / accounts => Thus, the result will be, for example, 1x 500 1x 50 1x 2

I made 2 arrays for this:

double[] CoinValue = {500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02,  0.01};
  uint[] CoinAmount = new uint[CoinValue.Length];

My problem is exactly how to β€œtell” the array the value 500 should be equal to 1 in the countAmount array. => 1. So, if I have 1000, the array array CoinAmount will know that it will need to hold 2 as a value (2x500 = 1000).

Thus, my end result will be something like this that gives the number of coins / bills: 1 x 500 1 x 50 1 x 2 .......

Thanks in advance.

+3
source share
4 answers

, . , ( ).

, , .

, , :

int number = (int)(total / sizeOfBill);

, /, , .

+4

: .

, , : "" , /. FYI, , , " "; , /.

, :

  • 1 = 60 ( "" - "" )
  • 1 = 30
  • 1 florin = 24 pence
  • 1 shilling = 12 pence
  • 1 tanner = 6 pence

, . , 48

  • , 18
  • , 6
  • ,

. , , 48 - , .

, ?

( , - , , !)

+2

decimal ; double :

    double value = 0.3;
    value -= 0.1;
    value -= 0.1;
    value -= 0.1;
    Console.WriteLine(value); //**not** zero

, ( , ) . , (.. 0.5M 0.2M, 0.1M 0.8M - 4x0.2M, 0.5M + 0.2M + (damn) )

    decimal value = 10023.23M;
    decimal[] CoinValue = { 500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5M, 0.2M, 0.1M, 0.05M, 0.02M, 0.01M };

    int[] counts = new int[CoinValue.Length];
    for (int i = 0; i < CoinValue.Length; i++) {
        decimal v = CoinValue[i];
        while (value >= v) {
            counts[i]++;
            value -= v;
        }
    }
    for (int i = 0; i < CoinValue.Length; i++) {
        if (counts[i] > 0) {
            Console.WriteLine(CoinValue[i] + "\t" + counts[i]);
        }
    }
    Console.WriteLine("Untendered: " + value);
+1

Given your array ... also works with decimals, of course.

double[] CoinValue = { 500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01 };

uint[] result = new uint[CoinValue.Length];
double ammount = 552.5;
double remaining = ammount;

for (int i = 0; i < CoinValue.Length; ++i) {
  result[i] = (uint) (remaining / CoinValue[i]);
    if (result[i] > 0)
      remaining = remaining % CoinValue[i];
}
0
source

Source: https://habr.com/ru/post/1734761/


All Articles