{"id":52,"date":"2008-05-03T23:24:33","date_gmt":"2008-05-03T15:24:33","guid":{"rendered":"http:\/\/www.zhuangbiaowei.cn\/?p=52"},"modified":"2008-05-03T23:24:33","modified_gmt":"2008-05-03T15:24:33","slug":"play-with-quiz-%e6%89%be%e9%9b%b6%e9%92%b12","status":"publish","type":"post","link":"https:\/\/blog.zhuangbiaowei.com\/?p=52","title":{"rendered":"Play with Quiz &#8212; \u627e\u96f6\u94b1(2)"},"content":{"rendered":"<p>\u63a5\u7740\u4e0a\u56de\u7684\u8ba8\u8bba\uff0c\u6211\u4eec\u9700\u8981\u5199\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u4e2a\u627e\u51fa\u6240\u6709\u7684\u96f6\u94b1\u7ec4\u5408\uff0cget_all_change_list\u3002\u53e6\u4e00\u4e2a\u4ece\u4e2d\u518d\u627e\u51fa\u7b26\u5408\u8981\u6c42\u7684\u4e00\u4e2a\u89e3\u3002<\/p>\n<p>\u627e\u51fa\u7b26\u5408\u8981\u6c42\u7684\u89e3\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u5148\u5199\u5728\u4e0b\u9762\u3002<br \/>\n[cc lang=&#8221;ruby&#8221; css=&#8221;no&#8221;]<br \/>\ndef get_best_change(change_list)<br \/>\n  best_change=nil<br \/>\n  min_length=100000<br \/>\n  change_list.each do |list|<br \/>\n    if list.length<min_length\n      best_change=list\n      min_length=list.length\n    end\n  end\n  return best_change\nend\n[\/cc]\n\n\u81f3\u4e8e\u627e\u5230\u6240\u6709\u7684\u89e3\uff0c\u5c31\u6bd4\u8f83\u9ebb\u70e6\uff0c\u4ece\u601d\u8def\u4e0a\u6765\u8bf4\uff0c\u53ef\u4ee5\u6709\u4e24\u4e2a\u65b9\u5411\uff0c\u4e00\u4e2a\u662f\u505a\u51cf\u6cd5\uff0c\u4e00\u4e2a\u662f\u505a\u52a0\u6cd5\u3002\u6240\u8c13\u51cf\u6cd5\uff0c\u5c31\u662f\u5047\u8bbe\u9700\u8981\u5151\u636215\u5143\u7684\u96f6\u94b1\uff0c\u6211\u5c31\u5148\u8003\u8651\u7b2c\u4e00\u4e2a\u786c\u5e01\u752810\u5143\uff0c\u63a5\u4e0b\u6765\u5c31\u6c42\u89e3\u5269\u4e0b5\u5143\u7684\u627e\u96f6\u89e3\u6cd5\u3002\u8fd9\u5c31\u662f\u4e00\u4e2a\u975e\u5e38\u81ea\u7136\u7684\uff0c\u9012\u5f52\u6c42\u89e3\u7684\u601d\u8def\u3002\u4ee3\u7801\u5982\u4e0b\uff1a\n\n[cc lang=\"ruby\" css=\"no\"]\ndef get_all_change_list(amount,coins)\n  change_list=[]\n  coins.each do |coin|\n    if amount>coin<br \/>\n      sub_change_list=get_all_change_list(amount-coin,coins)<br \/>\n      sub_change_list.each do |list|<br \/>\n        change_list.insert(-1,list.insert(-1,coin).sort)<br \/>\n      end<br \/>\n    end<br \/>\n    if amount==coin<br \/>\n      change_list.insert(-1,[coin])<br \/>\n    end<br \/>\n  end<br \/>\n  return change_list<br \/>\nend<br \/>\n[\/cc]<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u505a\u52a0\u6cd5\u7684\u601d\u8def\uff0c\u662f\u4ece\u6240\u6709\u786c\u5e01\u80fd\u591f\u5b8c\u6210\u7684\u7ec4\u5408\u6765\u7f57\u5217\u3002\u5047\u8bbe[25,10,5,1]\u8fd9\u6837\u4e00\u4e2a\u7ec4\u5408\uff0c\u90a3\u4e48\u4e00\u679a\u786c\u5e01\u7684\u7ec4\u5408\u65b9\u5f0f\uff0c\u5c31\u53ea\u67094\u4e2d\uff0c\u5206\u522b\u662f[25],[10],[5],[1]\uff0c\u90a3\u4e48\u4e24\u679a\u786c\u5e01\u7684\u7ec4\u5408\u65b9\u5f0f\u81ea\u7136\u5c31\u662f\uff0c[25,25],[25,10],[25,10]&#8230;.[1,5],[1,1]\u3002\u4e00\u517116\u79cd\uff0c\u53d6\u6389\u6b21\u5e8f\u4e0d\u540c\u7684\uff0c\u4e00\u5171\u670910\u79cd\u3002\u518d\u7ed9\u51fa\u6240\u6709\u96f6\u94b1\u7ec4\u5408\u7684\u57fa\u7840\u4e0a\uff0c\u518d\u5bfb\u627e\u7b26\u5408amount\u7684\u627e\u96f6\u7ec4\u5408\u5373\u53ef\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>[cc lang=&#8221;ruby&#8221; css=&#8221;no&#8221;]<br \/>\ndef get_all_change_list(amount,coins)<br \/>\n  min_coin=coins[coins.length-1]<br \/>\n  max_list_size=amount\/min_coin+ ((amount%min_coin==0) ? 0 : 1)<br \/>\n  change_list={}<br \/>\n  coins.each do |coin|<br \/>\n    change_list[[coin]]=coin<br \/>\n  end<br \/>\n  2.upto(max_list_size) do<br \/>\n    new_change_list={}<br \/>\n    coins.each do |coin|<br \/>\n      change_list.each do |list,v|<br \/>\n        new_change_list[list.clone.insert(list.length,coin).sort]=v+coin if v+coin<=amount\n      end\n    end\n    change_list.merge!(new_change_list)\n  end\n  change_list.delete_if { |key,value| value!=amount }\n  change_list.keys\nend\n[\/cc]\n\n\u5199\u51fa\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0c\u4e5f\u4e0d\u5bb9\u6613\uff0c\u5177\u4f53\u7684\u601d\u8def\uff0c\u660e\u5929\u518d\u8bb2\u89e3\u5427\u3002\u672a\u5b8c\u5f85\u7eed\u3002\u3002\u3002\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u7740\u4e0a\u56de\u7684\u8ba8\u8bba\uff0c\u6211\u4eec\u9700\u8981\u5199\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u4e2a\u627e\u51fa\u6240\u6709\u7684\u96f6\u94b1\u7ec4\u5408\uff0cget_all_change_list\u3002\u53e6\u4e00\u4e2a\u4ece\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,49],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-quiz","category-49"],"_links":{"self":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":0,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhuangbiaowei.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}