@Lin--
2020-02-05T05:22:57.000000Z
字数 830
阅读 455
Leetcode
题目分析:一个柠檬5元,有5,10,20的货币,一次售出一个柠檬,商家初始无货币若是。
若商家能找零钱,为true;否则,为false;
那么,题目有三种变量,5,10,20的货币;初始化值皆为0;
遍历数组,若是5元,则5元货币+1;若是10元,则10元货币+1,5元货币-1;若无5元货币,返回false;
若是20,则先10元货币-1,5元货币-1;若无10元货币,则5元货币-3;若无5元货币,返回false。
其余为true。
/**File:lemonadeChange.c:*Author:0HP*Date:20200202*Purpose:to solve the problem in Leetcode*https://leetcode.com/problems/lemonade-change/*//***input a array, which is the money, only include 5,10,20*Output whether we can return the charge**/#include<stdio.h>#include<stdbool.h>bool lemonadeChange(int* bills, int billsSize){int five=0,ten=0;for(int i=0;i<billsSize;++i){if(bills[i]==5){++five;}else if(bills[i]==10){--five;if(five<0){return false;}else{++ten;}}else{if(ten>0){--ten;--five;}else{five-=3;}if(ten<0||five<0){return false;}}}return true;}//testint main (){int a[5]={5,5,10,10,20};printf("%d",lemonadeChange(a,5));}