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 ...
随机推荐
- 让DIV浮动在表格上固定位置,不会随着显示器的分辨率变化。
<td> <div class="box"> <img src="/aa.jpg" /> <div class=&qu ...
- Linux 系统中用户切换(su user与 su - user 的区别)
1. Linux系统中用户切换的命令为su,语法为: su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]] 参 ...
- Sql Server CTE递归
WITH cte AS ( SELECT a.FNUMBER,a.FMATERIALID AS MainId,b.FMATERIALID AS ChileID,CAST(b.FMATERIALID A ...
- SQLServer触发器的使用
创建: create trigger trigger_name on {table_name view_name} {for After Instead of } [ insert, update,d ...
- ebay的api的开发技术笔记
使用eBay API基本步骤介绍 要开始使用eBay API,需要如下基本步骤: 1. 注册开发帐号: https://developer.ebay.com/join/Default.aspx ...
- Angularjs入门学习一 简介
本系列文章是从头开始学习angularjs,下文中用ng表示angularjs,要知道从以为根深蒂固的jquery开发者转变开发思想,确实需要一段时间,下面介绍以下 angularjs,我也是参考网上 ...
- format——MATLAB
format:设置输出格式 对浮点性变量,缺省为format short. format并不影响matlab如何计算和存储变量的值.对浮点型变量的计算,即单精度或双精度,按合适的浮点精度进行,而不论变 ...
- C++ 实现不能被继承的类
方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...
- linux系统环境变量.bash_profile/bashrc文件
系统环境变量的查看: [root@localhost ~]# envHOSTNAME=localhost.localdomainSELINUX_ROLE_REQUESTED=TERM=xtermSHE ...
- WebStorm 快捷键收藏
快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/-/ ) Ctrl+X 或 Ctrl+Y 删除一行 Shift+F6 重构-重命名 Alt+~vcs操作 Alt+~ 7关闭重 ...