import java.util.*;
public class Store {
    int cashList[][] = new int[][]{/*{面额,数量}*/{1,-1},{2,-1},{5,-1},{10,-1},{20,-1},{50,-1}};

    public void accept(int v) {//收钱
       for(int i=0;i<cashList.length;i++) {
           if(cashList[i][0] == v) {
               if(cashList[i][1]>=0) {               
                   cashList[i][1]++;
               }
               else {  
       cashList[i][1]=1;//初始化时数量为0不是更好,现在增加了循环体内判断,性能急剧下降
               }
           }                          
       }
    }
    
    public LinkedList return_charge(int total) {//找零
   //    assert(total>0);
       int r=-1;
       LinkedList chargeList = new LinkedList();
       for(int i=cashList.length-1;i>=0&&total!=0;) {
           total-=cashList[i][0]; 
           if(r<0){
               i--;  //当前面额太大,就选小面额的
               continue;
           }
           chargeList.add(new Integer(cashList[i][1]));
           cashList[i][1]--;
       }//chargeList中存放了零钱,chargeList.size()如果为0,就没钱找
       return chargeList;
    }
}

解决方案 »

  1.   

    不好意思写错了
    int r=-1这句没有
    if(r<0)改为if(total<0)
        public LinkedList return_charge(int total) {//找零
       //    assert(total>0);
           LinkedList chargeList = new LinkedList();
           for(int i=cashList.length-1;i>=0&&total!=0;) {
               total-=cashList[i][0]; 
               if(total<0){
                   i--;  //当前面额太大,就选小面额的
                   continue;
               }
               chargeList.add(new Integer(cashList[i][1]));
               cashList[i][1]--;
           }//chargeList中存放了零钱,chargeList.size()如果为0,就没钱找
           return chargeList;
        }
    }
      

  2.   

    再改一下
        public LinkedList return_charge(int total) {//找零
       //    assert(total>0);
           LinkedList chargeList = new LinkedList();
           for(int i=cashList.length-1;i>=0&&total!=0;) {
               total-=cashList[i][0]; 
               if(total<0 || cashList[i][1]<1){//面额大或者数量不足
                   i--;  //当前面额大或者数量不足,就选小面额的
                   continue;
               }
               chargeList.add(new Integer(cashList[i][1]));
               cashList[i][1]--;
           }//chargeList中存放了零钱,chargeList.size()如果为0,就没钱找
           return chargeList;
        }
    }
      

  3.   

    private void initRmbCount(){
    //.....;
    }
    int change = 87;
    int remain = 0;
    int temp = 0;
    int rmb[] = new int[6];
    int rmb_count[] = new int[6];
    int rmb_change_count[] = new int[6];
    rmb[0] = 50;
    rmb[1] = 20;
    rmb[2] = 10;
    rmb[3] = 5;
    rmb[4] = 2;
    rmb[5] = 1;
    remain = change;
    for (int i = 0; i < 6; i++)
    {
    if ( remain > rmb[i]){
    temp = remain % rmb[i];
    if ( temp > rmb_count[i] ) temp = rmb_count[i];
    rmb_change_count[i] = temp;
    remain = reamin - rmb_change_count[i]*rmb[i];
    if ( remmain == 0 ) break;
    } }

    if ( remain == 0){
    System.out.println("可以找零:找的钱为");
    for (int i =0 ; i < 6; i++){
    if (rmb_change_count[i] > 0 ){
    System.out.println("面值[" + rmb[i] + "]: " + rmb_change_count[i] + " 张");
    }
    } }else{
    System.out.println("Sorry,零钱不够!:");
    }
      

  4.   

    晕,贴上去怎么乱七八糟的,楼主自己看了。我写的主要是思路
    change = 87; 假设找87元
    remain 
    temp
    rmb[] //人民币币种
    rmb_count[]//现有数量
    rmb_change_count[]//各种币种找的数量
      

  5.   

    private void judgeSale()
         {
           int moneyType[] = {50,20,10,5,2,1};
           int stockMoneyNumber[] = {6,5,4,8,7,9};
           int payMoneyNumber[] = {1,1,2,2,1,3};
           int moneyNumber[] = new int[6];       int changeMoneyNumber[] = new int[6];
           int goodsPrice = 18;
           int payAmountOfMoney = 0;       for(int index = 0; index<6; index++)
           {
             moneyNumber[index] = stockMoneyNumber[index] + payMoneyNumber[index];
             payAmountOfMoney += (payMoneyNumber[index] * moneyType[index]);
           }       int changeMoneyAmount = payAmountOfMoney - goodsPrice;
           int surplusMoney = changeMoneyAmount;       for(int index = 0; index<6; index++)
           {
             changeMoneyNumber[index] = surplusMoney / moneyType[index];
             surplusMoney %= moneyType[index];
             if(changeMoneyNumber[index] > moneyNumber[index])
             {
               surplusMoney += ((changeMoneyNumber[index] - moneyNumber[index]) * moneyType[index]);
               changeMoneyNumber[index] = moneyNumber[index];
             }
           }       if(surplusMoney == 0)
           {
             System.out.print("客户所付钱币的总金额:");
             System.out.println(payAmountOfMoney);
             System.out.print("应找个客户的钱的总金额:");
             System.out.println(changeMoneyAmount);         for(int index = 0; index<6; index++)
             {
               stockMoneyNumber[index] = stockMoneyNumber[index] + payMoneyNumber[index] - changeMoneyNumber[index];
               System.out.print(moneyType[index]);
               System.out.print("number:");
               System.out.println(changeMoneyNumber[index]);
             }
           }
           else
           {
             System.out.println("不能找零!");
           }
         }//==============================================程序运行后的输出结果:客户所付钱币的总金额:105
    应找个客户的钱的总金额:87
    50number:1
    20number:1
    10number:1
    5number:1
    2number:1
    1number:0