{"id":78,"date":"2016-06-25T01:31:36","date_gmt":"2016-06-24T17:31:36","guid":{"rendered":"http:\/\/oprilzeng.com\/blog\/?p=78"},"modified":"2016-06-26T01:24:03","modified_gmt":"2016-06-25T17:24:03","slug":"leetcode-365-water-and-jug-problem","status":"publish","type":"post","link":"https:\/\/oprilzeng.com\/blog\/2016\/06\/leetcode-365-water-and-jug-problem\/","title":{"rendered":"Leetcode 365. Water and Jug Problem"},"content":{"rendered":"<p><strong>\u9898\u76ee\u63cf\u8ff0<\/strong><\/p>\n<p>You are given two jugs with capacities <i>x<\/i> and <i>y<\/i> litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly <i>z<\/i>\u00a0litres using these two jugs.<\/p>\n<p>Operations allowed:<\/p>\n<ul>\n<li>Fill any of the jugs completely.<\/li>\n<li>Empty any of the jugs.<\/li>\n<li>Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.<\/li>\n<\/ul>\n<p><strong>\u89e3\u9898\u601d\u8def<\/strong><\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5b9e\u8d28\u4e0a\u662f\u6c42\u89e3\u5173\u4e8ea\u548cb\u7684\u65b9\u7a0bax+by=z\u662f\u5426\u6709\u6574\u6570\u89e3\u7684\u95ee\u9898\u3002a\u548cb\u4e3a\u6b63\u6570\u65f6\u8868\u793a\u7ed9\u4e00\u4e2a\u7a7a\u58f6\u5012\u6ee1\u6c34\uff0c\u8d1f\u6570\u65f6\u8868\u793a\u628a\u4e00\u4e2a\u6c34\u58f6\u5012\u7a7a\u3002\u5bb9\u6613\u5f97\u77e5\u5f53\u4e14\u4ec5\u5f53z\u662fx\u4e0ey\u7684\u6700\u5c0f\u516c\u7ea6\u6570\u7684\u500d\u6570\u65f6\uff0c\u8be5\u65b9\u7a0b\u6709\u6574\u6570\u89e3\uff0c\u6240\u4ee5\u672c\u9898\u7684\u5173\u952e\u662f\u5224\u65adz\u88abx\u548cy\u7684\u6700\u5927\u516c\u7ea6\u6570\u6574\u9664\u5373\u53ef\uff0c\u5173\u952e\u7684\u7b97\u6cd5\u662f\u8f97\u8f6c\u76f8\u9664\u6cd5\u3002<\/p>\n<p>\u76ee\u524d\u5c1a\u5b58\u5728\u4e89\u8bae\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u5728\u6700\u540e\u91cf\u53d6\u7684\u6b65\u9aa4\u4e2d\u6c34\u58f6\u80fd\u5426\u88ab\u591a\u6b21\u4f7f\u7528\uff08\u6bd4\u5982x=1, y=2, z=5\u7684\u60c5\u51b5\uff0c\u662f\u5426\u53ef\u4ee5\u75281+2+2=5\u6765\u5b9e\u73b0\uff09\uff0c\u8be5\u95ee\u9898\u7684\u8ba8\u8bba\u533a\u91cc\u4e5f\u5728\u8ba8\u8bba\u4e2d\u3002\u76ee\u524d\u7684test cases\u597d\u50cf\u662f\u8ba4\u5b9a\u4e3a\u6700\u7ec8\u53ea\u80fd\u7528\u4e24\u4e2a\u6c34\u58f6\u5728\u67d0\u4e00\u65f6\u523b\u91cc\u5b58\u6709\u7684\u603b\u6c34\u91cf\u6765\u8861\u91cf\uff0c\u6240\u4ee5z&gt;x+y\u7684\u60c5\u51b5\u90fd\u5224\u5b9a\u4e3afalse\u4e86\u3002<\/p>\n<p><strong>\u4ee3\u7801<\/strong><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nclass Solution {\r\npublic:\r\n    int gcd(int x, int y)  \r\n    {\r\n        return y==0?x:gcd(y, x%y);  \r\n    }\r\n    bool canMeasureWater(int x, int y, int z) {\r\n        if(z &lt; 0 || z &gt; x + y)\r\n            return false;\r\n        if(z % gcd(x, y))\r\n            return false;\r\n        else\r\n            return true;\r\n    }\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 You are given two jugs with capacities x and y lit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":10,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprilzeng.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}