• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 21,  comments - 9,  trackbacks - 0

            硬幣找錢問(wèn)題

            問(wèn)題描述

            設(shè)有6種不同面值的硬幣,各硬幣的面值分別為5分,1角,2角,5角,1元,2元。現(xiàn)要用這些面值的硬幣來(lái)購(gòu)物和找錢。購(gòu)物時(shí)規(guī)定了可以使用的各種面值的硬幣個(gè)數(shù)。

            假定商店里各面值的硬幣有足夠多,顧客也可用多種方式支付。在1次購(gòu)物中希望使用最少硬幣個(gè)數(shù)。例如,1次購(gòu)物需要付款0.55元,沒(méi)有5角的硬幣,只好用2*20+10+5共4枚硬幣來(lái)付款。如果付出1元,找回4角5分,同樣需要4枚硬幣。但是如果付出1.05元(1枚1元和1枚5分),找回5角,只需要3枚硬幣。這個(gè)方案用的硬幣個(gè)數(shù)最少。

            您的任務(wù):對(duì)于給定的各種面值的硬幣個(gè)數(shù)和付款金額,計(jì)算使用硬幣個(gè)數(shù)最少的交易方案。

            輸入

            有若干行測(cè)試數(shù)據(jù)。每一行有6個(gè)整數(shù)a5、a4、a3、a2、a1、a0和1個(gè)有2位小數(shù)的實(shí)數(shù)money,分別表示5分,1角,2角,5角,1元,2元面值的硬幣個(gè)數(shù)和付款金額,money<=1000。文件以6個(gè)0結(jié)束(不必處理)。

            輸出

            對(duì)每一行測(cè)試數(shù)據(jù),一行輸出最少硬幣個(gè)數(shù)。如果不可能完成交易,則輸出“impossible”。

            輸入樣例

            2 4 2 2 1 0 0.95

            2 4 2 0 1 0 0.55

            0 0 0 0 0 0

            輸出樣例

            2

            3

              1 #include <iostream>
              2 #include<malloc.h>
              3 #include<fstream>
              4 using namespace std;
              5 ifstream ifs;
              6 ofstream ofs;
              7 
              8 
              9 int coins[6];//各面值硬幣數(shù)
             10 int count=0;//需要的最少硬幣數(shù)
             11 int cost,c[6]=510,20,50,100,200};//需支付數(shù)和硬幣面值,以分計(jì)算
             12 bool finish = false;
             13 void getData()
             14 {
             15     int flag = 0;
             16     for(int i=0;i<6;i++)
             17     {
             18         ifs>>coins[i];
             19         flag = flag | coins[i];
             20         
             21     }
             22     if(!flag)
             23     {
             24         finish = true;
             25         return ;
             26     }
             27     double costt;
             28     ifs>>costt;
             29     cost=(int)(costt*100);//將輸入的錢轉(zhuǎn)為 分
             30 }
             31 
             32 int contains(int a)
             33 {
             34     for(int i=0;i<6;i++)
             35     {
             36         if(c[i]==a&&coins[i]>0)return i;
             37     }
             38     return -1;
             39 }
             40 
             41 int getBackMin(int k)
             42 {
             43     int count = 0;
             44     for(int i = 5;i >=0;--i)
             45     {
             46         count += k /c[i];
             47         k = k % c[i];
             48     }
             49     return count;
             50 }
             51 
             52 //檢查一個(gè)數(shù)的后面是否還有硬幣,如果沒(méi)有了,或者余下的硬幣的和小于要找的值。那么就只能用當(dāng)前硬幣來(lái)計(jì)算了
             53 int checkLast(int index,int co)
             54 {
             55     int sum = 0;
             56     for(int i =index - 1;i >=0;--i )
             57     {
             58         sum += c[i] * coins[i];
             59     }
             60     if(co <= sum)
             61         return 1;
             62     else
             63         return 0;
             64 }
             65 
             66 void getMinCount()
             67 {
             68     for(int i=5;i>=0;i--)
             69     {
             70         if(coins[i]>0)
             71         {    int *cc=(int *)malloc((i+1)*sizeof(int));
             72             cc[0]=0;
             73             for(int k=1;k<i+1;k++)
             74             {
             75                 cc[k]=c[k-1];
             76             }
             77             for(int j=0;j<1+i;j++)
             78             {
             79         
             80                 if(cost>=(c[i]-cc[j]))
             81                 {
             82                     if(coins[i]>=cost/(c[i]-cc[j]))
             83                     {    count+=cost/(c[i]-cc[j])+1;
             84                 
             85                         if(contains(c[i]-cc[j])!=-1)
             86                         {
             87                             count--;
             88                             coins[contains(c[i]-cc[j])]-=cost/(c[i]-cc[j]);
             89                         }
             90                         else coins[i]-=cost/(c[i]-cc[j]);
             91                         cost=cost%(c[i]-cc[j]);    
             92                         if(contains(c[i])==-1)break;
             93                     }
             94                     else
             95                     {
             96                         count+=coins[i];
             97                         cost=cost-c[i]*coins[i];
             98                         coins[i]=0;
             99                     }
            100                 
            101                  
            102                 }
            103 
            104             }
            105             //剩下的錢不夠支付了
            106             if(!checkLast(i,cost))
            107             {
            108                 //檢查是否可以用當(dāng)前的幣值來(lái)支付
            109                 int hasLest = cost / c[i] + 1;
            110                 if(hasLest <= coins[i])
            111                 {
            112                     count += hasLest;
            113                     count += getBackMin(c[i] * hasLest - cost);
            114                     cost = 0;
            115                     break;                    
            116                 }
            117                 else
            118                     break; ;
            119             }
            120         }
            121 
            122     }
            123 
            124     if(count==0||cost!=0){ofs<<"impossible"<<endl;}
            125     else
            126     {
            127         ofs<<count<<endl;
            128     }
            129     
            130 }
            131 
            132 
            133 int main()
            134 {
            135     ifs.open("input.txt");
            136     ofs.open("output.txt");
            137     while(!finish)
            138     {
            139         count=0;
            140         getData();
            141         if(finish)
            142             break;
            143         getMinCount();
            144     }
            145     ifs.close();
            146     ofs.close();
            147     return 0;
            148 }


            posted on 2012-04-15 10:33 崔佳星 閱讀(2265) 評(píng)論(6)  編輯 收藏 引用

            FeedBack:
            # re: 硬幣找錢問(wèn)題
            2012-04-15 19:15 | 崔佳星
            以上算法不能求出正確解。  回復(fù)  更多評(píng)論
              
            # re: 硬幣找錢問(wèn)題
            2012-04-19 13:15 | 崔佳星
            以上算法沒(méi)有考慮一種情況,就是剩余幣值不足以支付。  回復(fù)  更多評(píng)論
              
            # re: 硬幣找錢問(wèn)題
            2012-04-19 15:40 | 崔佳星
            已經(jīng)修改了,可以過(guò)全部數(shù)據(jù)!!!!!  回復(fù)  更多評(píng)論
              
            # re: 硬幣找錢問(wèn)題
            2012-04-19 15:40 | 崔佳星
            2 4 2 2 1 0 0.95
            2 4 2 0 1 0 0.55
            2 4 2 2 0 0 0.95
            1 1 1 1 1 1 3.45
            1 1 1 1 1 1 3.85
            0 0 1 0 0 0 0.05
            0 0 1 0 0 0 0.20
            0 0 20 0 0 20 1.20
            0 0 20 0 0 20 4.20
            0 0 20 0 0 20 3.20
            0 10 10 0 0 10 1.15
            100 0 0 0 0 0 3.65
            5 5 5 5 0 5 1.15
            0 5 5 0 0 5 0.35
            10 10 10 10 10 10 0.05
            100 0 0 0 0 0 4.95
            0 0 0 0 0 0



            以上為測(cè)試數(shù)據(jù)  回復(fù)  更多評(píng)論
              
            # re: 硬幣找錢問(wèn)題
            2012-04-19 15:41 | 崔佳星
            2
            3
            3
            4
            6
            3
            1
            3
            3
            4
            5
            73
            4
            3
            1
            99


            以上為測(cè)試數(shù)據(jù)的正確結(jié)果  回復(fù)  更多評(píng)論
              
            # re: 硬幣找錢問(wèn)題[未登錄](méi)
            2012-04-19 17:15 | Shane
            搜著搜著居然找到這兒了,MARK。
            崔,用DP寫了沒(méi)?  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            <2012年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品综合一区二区三区| 成人综合久久精品色婷婷| 77777亚洲午夜久久多喷| 嫩草影院久久99| 2021国产精品久久精品| 久久精品夜夜夜夜夜久久| 久久午夜福利电影| 精品熟女少妇a∨免费久久| 久久夜色撩人精品国产| 国内精品久久久人妻中文字幕| 久久久久久亚洲精品无码| 精品久久久久久国产潘金莲| 综合久久一区二区三区 | 久久久国产打桩机| 久久不见久久见免费影院www日本| 香蕉久久久久久狠狠色| 久久夜色精品国产亚洲| 麻豆成人久久精品二区三区免费 | 性高湖久久久久久久久AAAAA| 久久精品国产91久久综合麻豆自制 | 欧美精品久久久久久久自慰| 久久久久久久综合狠狠综合| 国产精品久久久久久久久久免费| 久久久久亚洲AV无码麻豆| 午夜精品久久久久久毛片| 午夜精品久久久久| 青青草原综合久久大伊人| 污污内射久久一区二区欧美日韩 | 欧美久久综合性欧美| 久久久久久久亚洲Av无码| 亚洲狠狠婷婷综合久久久久| 久久精品人人做人人爽电影| 欧美亚洲国产精品久久久久| 国产69精品久久久久观看软件 | 色8久久人人97超碰香蕉987| 亚洲精品白浆高清久久久久久| 久久久久久久久波多野高潮| 国产精品美女久久福利网站| 日产精品99久久久久久| 国产精品久久久久影院色| 国产精品免费久久久久电影网|