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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. 慎用memset();

    <span style="font-family: Arial, Helvetica, sans-serif;">void *(memset) (void *s,int ...

  2. JavaScript之动态背景登陆表单

    <!doctype html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. MySQL字符集编码的理解分析

    今天帮同事处理一个棘手的事情,问题是这样的: 无论在客户机用哪个版本的mysql客户端连接服务器,发现只要服务器端设置了 character-set-server = utf8之后, characte ...

  4. 实现百度外卖APP个人中心头像"浪"起来的动画效果

    让你的头像浪起来~~~~~ DEMO 地址:网页链接,点击下载 你需要知道的 CADisplayLink 简单的说就是一定时器,其根本利用刷帧和屏幕频率一样来重绘渲染页面. 其创建方式: [Objec ...

  5. Row_Number实现分页(适用SQL)

    1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...

  6. 《HTML5与CSS3基础教程》学习笔记 ——Four Day

    第十六章 1.    输入和元素 电子邮件框 <input type="email"> 搜索框 <input type="search"> ...

  7. QQ截图工具提取

    今晚关了QQ,突然想截个图,但是呢又不想打开QQ了,于是在网上搜索截图工具,下载了几个,感觉都没有QQ截图好用.索性直接百度QQ截图工具提取,看到有些网站上有提取的,里面的文件有点多,不是我中意的,突 ...

  8. java 自动装箱和自动拆箱

    自动装箱 java执行Integer i = 100:编译器编译成Integer i = Integer.valueOf(100); Integer i = 100; //编译器编译成Integer ...

  9. C++调用GDAL库读取并输出tif文件,并计算斑块面积输出景观指数:CSD

    部分源码选自GDAL库的官方网址:www.gdal.org,其余的代码为笔者自己编写. // readfile.cpp : 定义控制台应用程序的入口点. // /* part of the codes ...

  10. c++11之右值引用

    本文大部分来自这里,并不是完全着行翻译,如有不明白的地方请参考原文. 在c++中,创建临时对象的开销对程序的影响一直很大,比如以下这个例子: String getName(){ return “Kia ...