From b3e1afed81abfbfeca738dcc6e106a33678e6564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=B5=B7=E6=B4=B2?= Date: Thu, 20 Sep 2018 21:05:06 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E6=80=BB=E7=BB=93.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加小米校招2018笔试题第一题 --- notes/面试总结.md | 65 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/notes/面试总结.md b/notes/面试总结.md index 8519efdd..479b25b2 100644 --- a/notes/面试总结.md +++ b/notes/面试总结.md @@ -1,6 +1,6 @@ * [1. 字符串组合](#1-字符串组合) -* [2. 实现 Singleton](#2-实现-singleton) +* [2. 整数组合求和](#2-整数组合求和) * [3. 数组中重复的数字](#3-数组中重复的数字) * [参考文献](#参考文献) @@ -64,10 +64,69 @@ ret_list = list(set(result)^set(result_same)) print len(ret_list) ``` +# 2. 整数组合求和 -# 2. 实现 Singleton +## 题目描述 + +小米之家是成人糖果店。里面有很多便宜,好用,好玩的产品。中秋节到了,小米之家想给米粉准备一些固定金额大礼包。对于给定的一个金额,需要判断能不能用 +不同种产品(一种产品在礼包最多出现一次)组合出来这个金额。聪明的你来帮帮米家的小伙伴吧。 + +### 输入 +```code +输入N(N是正整数, N < = 200) +输入N个价格p(正整数, p <= 10000)用单空格分割 +输入金额M(M是正整数,M <= 100000) +``` + +### 输出 +```code +能组合出来输出1 +否则输出0 +``` + +### 样例输入 +```code +6 +99 199 1999 10000 39 1499 +10238 +``` + +### 样例输出 +```code +1 +``` + +## 解题思路 +本题采用一种比较直接的方式进行解题,分为如下步骤: +1. 对输入np进行排序,方便后面快速结束。 +2. 求解给定np时候的0~n个组合的和,提供`Python`提供的`itertools.permutations`来实现。 +3. 如果较小的数相加已经大于目标,可以提前跳出本次循环。 + + +```python +import itertools + +n = int(raw_input()) + + +np = [int(k) for k in raw_input().split(" ")] +np.sort() + +sum_np = int(raw_input()) + +flag = 0 + +for i in range(n): + for j in itertools.permutations(np, i): + if sum(j) > sum_np: + continue; + if sum(j) == sum_np: + flag = 1; + break; + +print flag +``` -[单例模式](https://github.com/CyC2018/Interview-Notebook/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md) # 3. 数组中重复的数字