Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40
原题:
Given a number N, write a program that returns all possible combinations of numbers that add up to N, as lists. (Exclude the N+=N)
For example, if N= return {{,,,},{,,},{,},{,}}
题目:给定一个正整数N,求出所有的由正整数加起来等于N的可能组合(加数保持升序)。比如N = 4的时候,结果是{{1,1,1,1},{1,1,2},{2,2},{1,3}}。
解法:递归解决即可。
代码:
// http://www.careercup.com/question?id=6321181669982208
#include <iostream>
#include <vector>
using namespace std; void DFS(int cur, int remain, vector<vector<int> > &result, vector<int> &sum)
{
if (remain == ) {
result.push_back(sum);
return;
}
if (remain < cur) {
return;
} int i;
for (i = cur; i <= remain; ++i) {
if (remain - i != && remain - i < i) {
continue;
}
sum.push_back(i);
DFS(i, remain - i, result, sum);
sum.pop_back();
}
} int main()
{
int n;
vector<int> sum;
vector<vector<int> > result;
int i, j; while (cin >> n && n > ) {
DFS(, n, result, sum); cout << "{" << endl;
for (i = ; i < (int)result.size(); ++i) {
cout << " {";
for (j = ; j < (int)result[i].size(); ++j) {
j ? cout << ", ", : ;
cout << result[i][j];
}
cout << "}" << endl;
}
cout << "}" << endl; sum.clear();
for (i = ; i < (int)result.size(); ++i) {
result[i].clear();
}
result.clear();
} return ;
}
Careercup - Facebook面试题 - 6321181669982208的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- css样式编辑
1.剪切: clip:rect(a,b,c,d); a参数:从顶部开始剪切,多少参数表示腰间切多少: b参数:从右边开始剪切,用减法来计算要剪去的多少:(最大的宽度-b参数 = 剪去的参数) c参数: ...
- Table of Contents - JMS
JMS Specification v1.1 JMS 基本概念 Message QueueBrowser 消息选择器 消息确认 ConnectionMetaData ExceptionListener ...
- WinFrom“动态”WebService
1.首先添加一个WebService: 2.输入地址....Ok: 3.在WebService用到的类上按F12: 4.进入类中,找到其构造函数: 5.修改其构造函数为代参数,并且让this.Url= ...
- 安卓、java开发软件官网和相关不错的网站软件下载地址
java:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html eclipse:htt ...
- Android DiffUtil
Android 的recyclerview-v7:24.2.0 发布后多了个DiffUtil工具类,这个工具类能够大大解放了Android开发者的一个苦恼:RecyclerView局部刷新和重新刷新时 ...
- JavaScript之放大镜效果
在网上也浏览过许多关于JavaScript放大镜效果的文章,有的代码解释得些隐晦难懂,看的我头有点晕晕的╮(╯﹏╰)╭,我的心情是这样的: 吐槽完了,我们动动小鼠标,当鼠标经过下面这张美女图片时就实现 ...
- vpn连接成功后,本地无法连接外网
把在远程网络上使用默认网关前面的对勾取消掉,确定就ok啦...
- highcharts 图表库的简单使用
Highcharts简介: Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图 ...
- 设计main函数退出后继续执行一段代码
原理: 使用 _onexit() 函数注册一个函数,这个函数会在main函数退出后执行 使用原则: 1.包含在cstdlib中,是c语言中的库函数: 2.需要注册的函数格式为:int类型返回值.无参数 ...
- 使用Hint来优化执行计划
最近看主管优化了一个HINT相关的查询 借此机会学习下HINT 参考Notes: Note 129385 - Database hints in Open SQL http://www.stechno ...