ATM程序,无法弄清楚如何分解提款金额

时间:2016-07-25 16:18:20

标签: c#

首先是的,这是一个家庭作业,我一直在努力奋斗3天,我无法理解。

基本上问题是在文本框中输入用户输入的小数,然后我需要取这个数字并将其分解为货币面额,50美元,20美元,10美元,5美元,1美元,如果金额有然后进入小数 $ .25,$ .10,$ .05,$ .01。

我需要以尽可能低的面额来减少这种情况,例如100美元将被分解为2个50美元的账单。

这是我到目前为止所拥有的。

private void btnDispense_Click(object sender, EventArgs e)
{
    decimal i;
    i = decimal.Parse(txtAmountReq.Text);
    decimal totalAmount = Convert.ToDecimal(txtAmountReq);

    int[] denomBills = { 50, 20, 10, 5, 1 };
    int[] numberOfBills = new int[5];
    decimal[] denomCoins = { .25m, .10m, .05m, .01m };
    int[] numberOfCoins = new int[4];

    //For loop for amount of bills
    for (numberOfBills[0] = 0; totalAmount >= 50; numberOfBills[0]++)
    {
        totalAmount = totalAmount - 50;
    }
    for (numberOfBills[1] = 0; totalAmount < 20; numberOfBills[1]++)
    {
        totalAmount = totalAmount - 20;
    }
    for (numberOfBills[2] = 0; totalAmount < 10; numberOfBills[2]++)
    {
        totalAmount = totalAmount - 10;
    }
    for (numberOfBills[3] = 0; totalAmount < 5; numberOfBills[3]++)
    {
        totalAmount = totalAmount - 5;
    }
    for (numberOfBills[4] = 0; totalAmount <= 0; numberOfBills[4]++)
    {
        totalAmount = totalAmount - 1;
    }


    //For loop for the amount of coins
    for (numberOfCoins[0] = 0; totalAmount >= .25m; numberOfBills[0]++)
    {
        totalAmount = totalAmount - .25m;
    }
    for (numberOfBills[1] = 0; totalAmount < .10m; numberOfBills[1]++)
    {
        totalAmount = totalAmount - .10m;
    }
    for (numberOfBills[2] = 0; totalAmount < .05m; numberOfBills[2]++)
    {
        totalAmount = totalAmount - .05m;
    }
    for (numberOfBills[3] = 0; totalAmount < .01m; numberOfBills[3]++)
    {
        totalAmount = totalAmount - .01m;
    }

    txt50.Text = Convert.ToString(numberOfBills[0]);
    txt20.Text = Convert.ToString(numberOfBills[1]);
    txt10.Text = Convert.ToString(numberOfBills[2]);
    txt5.Text = Convert.ToString(numberOfBills[3]);
    txt1.Text = Convert.ToString(numberOfBills[4]);

    txtQuarter.Text = Convert.ToString(numberOfCoins[0]);
    txtDime.Text = Convert.ToString(numberOfCoins[1]);
    txtNickel.Text = Convert.ToString(numberOfCoins[2]);
    txtPenny.Text = Convert.ToString(numberOfCoins[3]);
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是非常有名的背包式问题。你可能想从这里开始探索:https://en.wikipedia.org/wiki/Change-making_problem

解决此问题的最佳方法是找到所有可能的更改组合,然后找到最佳解决方案。 下面是我在codeproject.com上找到的karamana的代码:

//find all the combinations
    private void findAllCombinationsRecursive(String tsoln,
              int startIx,
              int remainingTarget,
            CoinChangeAnswer answer) {
        for(int i=startIx; i<answer.denoms.length ;i++) {
            int temp = remainingTarget - answer.denoms[i];
            String tempSoln = tsoln + "" + answer.denoms[i]+ ",";
            if(temp < 0) {
             break;
            }
            if(temp == 0) {
             // reached the answer hence quit from the loop
             answer.allPossibleChanges.add(tempSoln);
             break;
            } 
            else {
            // target not reached, try the solution recursively with the
            // current denomination as the start point.
             findAllCombinationsRecursive(tempSoln, i, temp, answer);
            }
         }
     }

为了找到最佳解决方案:

public CoinChangeAnswer findOptimalChange(int target, int[] denoms) {
 CoinChangeAnswer soln = new CoinChangeAnswer(target,denoms);
 StringBuilder sb = new StringBuilder();

 // initialize the solution structure
 for(int i=0; i<soln.OPT[0].length ; i++) {
     soln.OPT[0][i] = i;
     soln.optimalChange[0][i] = sb.toString();
     sb.append(denoms[0]+" ");
 }

 // Read through the following for more details on the explanation
 // of the algorithm.
 // http://condor.depaul.edu/~rjohnson/algorithm/coins.pdf
 for(int i=1 ; i<denoms.length ; i++) {
     for(int j=0; j<target+1 ; j++) {
      int value = j;
      int targetWithPrevDenomiation = soln.OPT[i-1][j];
      int ix = (value) - denoms[i];
      if( ix>=0 && (denoms[i] <= value )) {
          int x2 = denoms[i] + soln.OPT[i][ix];
          if(x2 <= target && (1+soln.OPT[i][ix] < targetWithPrevDenomiation))            {
           String temp = soln.optimalChange[i][ix] + denoms[i] + " ";
           soln.optimalChange[i][j] = temp;
           soln.OPT[i][j] = 1 + soln.OPT[i][ix];
          } else {
           soln.optimalChange[i][j] = soln.optimalChange[i-1][j]+ " ";
           soln.OPT[i][j] = targetWithPrevDenomiation;
          }
      } else {
          soln.optimalChange[i][j] = soln.optimalChange[i-1][j];
          soln.OPT[i][j] = targetWithPrevDenomiation;
      }
    }
 }
 return soln;

}

链接到原始代码here