From 5a851142f15179d271d88081d17e41957636c58d Mon Sep 17 00:00:00 2001 From: ArthurP Date: Wed, 22 Sep 2021 09:40:55 +0100 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 8e20c402..bae1566c 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -217,6 +217,29 @@ var findContentChildren = function(g, s) { ``` +C: +```c +int cmp(int* a, int* b) { + return *a - *b; +} + +int findContentChildren(int* g, int gSize, int* s, int sSize){ + if(sSize == 0) + return 0; + + //将两个数组排序为升序 + qsort(g, gSize, sizeof(int), cmp); + qsort(s, sSize, sizeof(int), cmp); + + int numFedChildren = 0; + int i = 0; + for(i = 0; i < sSize; ++i) { + if(numFedChildren < gSize && g[numFedChildren] <= s[i]) + numFedChildren++; + } + return numFedChildren; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)