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 ...
随机推荐
- JS内存泄露常见原因
详细内容请点击 分享的笔记本-前端 开发中,我们常遇见的一些关于js内存泄露的问题,有时候我们常常会找半天找不出原因,这里给大家介绍简单便捷的方法 1.闭包上下文绑定后没有释放: 2.观察者模式在 ...
- 微信小程序开发1
关于微信小程序的开发.对于我们这些没学过oc或者android的人来说,无疑是一个令人鸡冻的好消息.这段时间研究了微信小程序开发.关于小程序的注册,认证和基础环境的搭建,官方文档已经非常详细了.这里就 ...
- Ajax中get提交和post提交的区别
$.post("HandlerLiuYan.ashx", { leixing: leixing, danwei: danwei, liuyan: liuyan, name: nam ...
- Android-短信验证
一.mob.com移动开发者服务平台(ShareSDK)的认识 该平台主要是致力于解决移动开发者的实际需求,同时也致力于一些第三方平台的框架支持,那么这样我们可以更方便的将一些功能集成到我们的App中 ...
- Linux下向SVN服务器添加新文件步骤
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain ...
- (转)Android面试题
1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存 B.内存回收程序负责释放无用内存 C.内存回收程序允许程序员直接释放内存 D.内存回收程序可以在 ...
- ArrayList与List对象用法与区别
比如: 代码如下 复制代码 string[] s=new string[3];//赋值s[0]="a";s[1]="b";s[2]="c"; ...
- iOS-GCD多线程
GCD GCD -- Grand Central Dispatch 是基于C语言的底层API 用Block定义任务,将任务添加到线程中使用.集中管理 1.GCD的执行函数 //同步 dispatch_ ...
- c# 读取远程主机性能计数器
PerformanceCounter pc = new PerformanceCounter("Web Service", "Current Connections&qu ...
- 了解常见的开源协议(BSD, GPL, LGPL,MIT)
一直对各种开源协议比较模糊, 特意在网上搜索了一下资料, 整理总结,以作记录 如果不喜欢长篇大论的话, 看下图就可以了 基本概念了解: 1. Contributors 和 Recipients Con ...