From 84750aca45f96f15e4dc271410c32cde78667e41 Mon Sep 17 00:00:00 2001 From: 243wresfdxvc Date: Sun, 24 Apr 2022 18:32:09 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200860.=E6=9F=A0=E6=AA=AC?= =?UTF-8?q?=E6=B0=B4=E6=89=BE=E9=9B=B6.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0860.柠檬水找零.md | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index ffd5490d..2738f574 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -250,6 +250,49 @@ var lemonadeChange = function(bills) { return true }; +``` +### C +```c +bool lemonadeChange(int* bills, int billsSize){ + // 分别记录五元、十元的数量(二十元不用记录,因为不会用到20元找零) + int fiveCount = 0; int tenCount = 0; + + int i; + for(i = 0; i < billsSize; ++i) { + // 分情况讨论每位顾客的付款 + switch(bills[i]) { + // 情况一:直接收款五元 + case 5: + fiveCount++; + break; + // 情况二:收款十元 + case 10: + // 若没有五元找零,返回false + if(fiveCount == 0) + return false; + // 收款十元并找零五元 + fiveCount--; + tenCount++; + break; + // 情况三:收款二十元 + case 20: + // 若可以,优先用十元和五元找零(因为十元只能找零20,所以需要尽量用掉。而5元能找零十元和二十元) + if(fiveCount > 0 && tenCount > 0) { + fiveCount--; + tenCount--; + } + // 若没有十元,但是有三张五元。用三张五元找零 + else if(fiveCount >= 3) + fiveCount-=3; + // 无法找开,返回false + else + return false; + break; + } + } + // 全部可以找开,返回true + return true; +} ``` -----------------------