创新工场二面试题!已经跪了,求解答!

yangxuefeng09 2015-09-29 09:00:09
加精
创新工场二面的面试官,问了我一个题目,我直接就跪在那里了!题目如下:

4=1+1+1+1
=1+1+2
=1+3
=2+2

比如 数字4 可以被拆解成为如上的四种情况。那么我现在给你一个vector< vector<int> > 你把所有的结果全部的保存到这个

vector< vector<int> > 中。

函数的函数体,应该是这个样子的 vector< vector<int> > & fun(int n){ //函数体 }

让我写出这个函数的代码,但是我写不出来,甚至连算法我 都没有思路。

可能大家觉的4还是很容易的 ,但是我给你100,那么:
100=59+41
=58+42
=57+43
。。。。
=1+99
=1+1+98
=1+1+97

等等 结果非常的多 我也不知道该如何拆分


求前辈解答

面试跪了 ,跪的心服口服 但是 这个题目还是不会 求帮助
...全文
11393 87 打赏 收藏 转发到动态 举报
写回复
用AI写文章
87 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhm357101232 2019-07-18
  • 打赏
  • 举报
回复
using namespace std;

vector<vector<int>> result;

vector<vector<int>>& func(int n) {
if (n == 1) {
result.clear();
vector<int> a = { 1 };
result.push_back(a);
return result;
}
else {
result = func(n - 1);

vector<vector<int>> r2;

for (int i = 1; i < n+1; i++) {
for (auto& iter : result) {
if (iter.end()- iter.begin() < i) break;
if (i < (*(iter.begin()+i-1)) + 1) {
int sum = i;
vector<int> b;
for (auto it = (iter.begin() + i - 1); it < iter.end(); it++) sum += *it;
if (sum == n) {
b.push_back(i);
b.insert(b.end(), (iter.begin() + i - 1), iter.end());
r2.push_back(b);
}
}
}

}
result = r2;
vector<int> a = { n };
result.push_back(a);
return result;
}

}

int main()
{
for (auto iter : func(10)) {
for (auto subitor : iter) {
cout << subitor << ",";
}
cout << endl;
}


return 0;

}

1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,2,
1,1,1,1,1,1,1,3,
1,1,1,1,1,1,2,2,
1,1,1,1,1,1,4,
1,1,1,1,1,2,3,
1,1,1,1,1,5,
1,1,1,1,2,2,2,
1,1,1,1,2,4,
1,1,1,1,3,3,
1,1,1,1,6,
1,1,1,2,2,3,
1,1,1,2,5,
1,1,1,3,4,
1,1,1,7,
1,1,2,2,2,2,
1,1,2,2,4,
1,1,2,3,3,
1,1,2,6,
1,1,3,5,
1,1,4,4,
1,1,8,
1,2,2,2,3,
1,2,2,5,
1,2,3,4,
1,2,7,
1,3,3,3,
1,3,6,
1,4,5,
1,9,
2,2,2,2,2,
2,2,2,4,
2,2,3,3,
2,2,6,
2,3,5,
2,4,4,
2,8,
3,3,4,
3,7,
4,6,
5,5,
10,
yhm357101232 2019-07-18
  • 打赏
  • 举报
回复
using namespace std;

vector<vector<int>> result;

vector<vector<int>>& func(int n) {
if (n == 1) {
result.clear();
vector<int> a = { 1 };
result.push_back(a);
return result;
}
else {
result = func(n - 1);

vector<vector<int>> r2;

for (int i = 1; i < n+1; i++) {
for (auto& iter : result) {
if (iter.end()- iter.begin() < i) break;
if (i < (*(iter.begin()+i-1)) + 1) {
int sum = i;
vector<int> b;
for (auto it = (iter.begin() + i - 1); it < iter.end(); it++) sum += *it;
if (sum == n) {
b.push_back(i);
b.insert(b.end(), (iter.begin() + i - 1), iter.end());
r2.push_back(b);
}
}
}

}
result = r2;
vector<int> a = { n };
result.push_back(a);
return result;
}

}

int main()
{
for (auto iter : func(10)) {
for (auto subitor : iter) {
cout << subitor << ",";
}
cout << endl;
}


return 0;

}

1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,2,
1,1,1,1,1,1,1,3,
1,1,1,1,1,1,2,2,
1,1,1,1,1,1,4,
1,1,1,1,1,2,3,
1,1,1,1,1,5,
1,1,1,1,2,2,2,
1,1,1,1,2,4,
1,1,1,1,3,3,
1,1,1,1,6,
1,1,1,2,2,3,
1,1,1,2,5,
1,1,1,3,4,
1,1,1,7,
1,1,2,2,2,2,
1,1,2,2,4,
1,1,2,3,3,
1,1,2,6,
1,1,3,5,
1,1,4,4,
1,1,8,
1,2,2,2,3,
1,2,2,5,
1,2,3,4,
1,2,7,
1,3,3,3,
1,3,6,
1,4,5,
1,9,
2,2,2,2,2,
2,2,2,4,
2,2,3,3,
2,2,6,
2,3,5,
2,4,4,
2,8,
3,3,4,
3,7,
4,6,
5,5,
10,
虎背人腰_ 2017-10-10
  • 打赏
  • 举报
回复
不错,还是蛮好的~
Crazy_Bookman 2016-05-14
  • 打赏
  • 举报
回复

#include "Header.h"
vector<vector<int> > func(int n)
{
	vector<vector<int> > groups;
	if(n == 1) groups.push_back(vector<int>(1,1));
	else
	{
		for(int i = 1; i != n/2 + 1; ++i)
		{
			vector<int> comb;
			for(int j = i; j != n-i+1; ++j)
			{
				comb.push_back(i);
				for(int k = 0; k != n - i - j; ++k)
					comb.push_back(1);
				comb.push_back(j);
				groups.push_back(comb);
				comb.clear();
			}		
		}	
	}	
	return groups;	
}
Crazy_Bookman 2016-05-14
  • 打赏
  • 举报
回复
引用 36 楼 micropentium6 的回复:
楼上的各位大神,不带你们肿么玩的啊!写个代码你们都不测试啊?木有一个是对的!csdn这是肿么了! 当然,赵老师的我没敢看,看也看不懂! 10的分解有42种,凡是得不到这个数的,都罚十一加班20天!
可以去试试我的,测试过了
jakey0108 2016-04-01
  • 打赏
  • 举报
回复
确实牛逼,大神真多,学习了
zeroandexe 2016-03-30
  • 打赏
  • 举报
回复
vector<vector<int>> fun(int n){ vector<vector<int>> answer; for (int i = 1; i <= n/2; i++){ int max_j = n - i; for (int j = i; j <= max_j; j++){ int count_one = max_j - j; vector<int> temp; while (count_one--) temp.push_back(1); temp.push_back(j); temp.push_back(i); sort(temp.begin(), temp.end()); answer.push_back(temp); } } return answer; }
奋起无迹 2015-11-07
  • 打赏
  • 举报
回复
研究下
w826207 2015-11-03
  • 打赏
  • 举报
回复
http://blog.chinaunix.net/uid-26548237-id-3503956.html
HaoYuan 2015-10-18
  • 打赏
  • 举报
回复
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<stdlib.h> #include <stdio.h> #include <vector> using namespace std; int g_Count = 0; void func(int n, vector<int>& vec) { if (n == 0) { if (vec.size() == 1) return; for (auto it = vec.begin(); it != vec.end(); it++) { printf("%3d", *it); } printf("\n"); g_Count++; } int iSize = vec.size(); for (int i = 1; i <= n; i++) { if (iSize > 0 && vec[iSize - 1] > i)//去重 continue; vec.push_back(i); func(n - i, vec); vec.pop_back(); } } int main() { int n = 10; vector<int> vect; func(n, vect); printf("一共%d个\n", g_Count); system("pause"); return 0; }
hy890319 2015-10-16
  • 打赏
  • 举报
回复
值得学习哦,感谢各位
唱跳rap敲代码 2015-10-15
  • 打赏
  • 举报
回复
package com.test;

import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;

public class MainRun {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int input = scanner.nextInt();
		Vector<Vector<Integer>> result = new Vector<Vector<Integer>>();
		calc(result, input);
		for (Vector vec : result) {
			for (int i = 0; i < vec.size(); i++)
				System.out.print(vec.get(i) + " ");
			System.out.println();
		}
	}

	public static void calc(Vector<Vector<Integer>> vec, int n) {
		calc_(vec, n, 1, new Stack<Integer>());
	}

	private static void calc_(Vector<Vector<Integer>> vec, int n, int floor,
			Stack<Integer> tmp) {
		boolean end = false;
		for (int i = 1; i <= n; i++) {
			if (i < floor)
				continue;
			floor = i;
			tmp.push(i);
			calc_(vec, n - i, floor, tmp);
			tmp.pop();
			end = true;
		}
		if (end) {
			Vector<Integer> success = new Vector<Integer>();
			for (int i = 0; i < tmp.size(); i++)
				success.add(tmp.get(i));
			success.add(n);
			vec.add(success);
		}
	}
}
walkuere 2015-10-15
  • 打赏
  • 举报
回复
我觉得应该先好几个小数字手算,算出用排列组合的公式,直接用排列组合的公式来,而不是无脑地递归枚举
小晏 2015-10-14
  • 打赏
  • 举报
回复
写个数学方程式就出来了,没意思
whao_mobile 2015-10-14
  • 打赏
  • 举报
回复
def class 
puts 'hello word!'
end
gpbgpbgpb 2015-10-13
  • 打赏
  • 举报
回复
写了个JS版的,一个递归,一个是递归转的循环实现。不用编译,直接打开浏览器运行,在FIREFOX调试器里查看LOG:
=========================================================
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<script>
function test1 () {
var N = 10;
var deepLevel = 0;
var xVarArray = new Array(N);
//initialization xVarArray to Zero.
for (var idx = 0; idx < N; idx++) {
xVarArray[ idx ] = 0;
}

function condition () {
var sumOfTotal = 0;
for (var i = 0; i < deepLevel; i++) {
sumOfTotal += xVarArray[ i ];
}
if (sumOfTotal === N) return 'match';
else if (sumOfTotal < N) return 'less';
else return 'overflow';
}

function push (number) {
if (deepLevel < N) {
xVarArray[ deepLevel ] = number;
deepLevel++;
}
}

function getCurrent () {
return xVarArray[ deepLevel - 1 ];
}

function increaseCurrent () {
xVarArray[ deepLevel - 1 ] = xVarArray[ deepLevel - 1 ] + 1;
}

function pop () {
deepLevel--;
}

function LOOP () {
//initial first Var
push(1);
var needPush = true;
while ( true ) {
if (needPush) push(getCurrent());
var cond = condition();
if (cond === 'match') {
console.log("output xVarArray=> " + xVarArray.slice(0, deepLevel));
increaseCurrent();
needPush = false;
//while loop ended condition weird?
if (xVarArray.slice(0, deepLevel).length === 1) return;
}
else if (cond === 'less') {
needPush = true;
}
else if (cond === 'overflow') {
pop();
increaseCurrent();
needPush = false;
}
}
}

LOOP();
}

function test2 () {
var N = 10;
var deepLevel = 0;
var xVarArray = new Array(N);
//initialization xVarArray to Zero.
for (var idx = 0; idx < N; idx++) {
xVarArray[ idx ] = 0;
}

function condition () {
var sumOfTotal = 0;
for (var i = 0; i < deepLevel; i++) {
sumOfTotal += xVarArray[ i ];
}
if (sumOfTotal === N) return 'match';
else if (sumOfTotal < N) return 'less';
else return 'overflow';
}

//convert Multiple embedded For Loop to recursive Call.
function RC (numStart) {
deepLevel++;
for (var i = numStart; i <= N; i++) {
xVarArray[ deepLevel - 1 ] = i;
var cond = condition();
if (cond === 'match') {
console.log("output xVarArray=> " + xVarArray.slice(0, deepLevel));
}
else if (cond === 'less')
RC(i);
else if (cond === 'overflow') break;
}

deepLevel--;
}

RC(1);
}
</script>

<body>
<button onclick="test1()">Test</button>
</body>
</html>
Conmajia 2015-10-12
  • 打赏
  • 举报
回复
满满的ACM即视感。。。
czg2020czg 2015-10-12
  • 打赏
  • 举报
回复
路过,看看就行 ,貌似很难得样子
pujihuai4 2015-10-11
  • 打赏
  • 举报
回复
进来学习了呢。。。。。。。。。。。
xutao1196 2015-10-11
  • 打赏
  • 举报
回复
这个有点意思,
加载更多回复(67)
作者:July、阿财。 时间:零一一年十月十三日。 ------------------------------ 无私分享造就开源的辉煌。 今是零一一年十月十三日,明日14日即是本人刚好开博一周年。在一周年之际,特此分享出微软面试 全部100题案的完整版,以作为对本博客所有读者的回馈。 一年之前的10月14日,一个名叫July 的人在一个叫csdn 的论坛上开帖分享微软等公司数据结构+算法 面试100题,自此,与上千网友一起做,一起思考,一起解这些面试题目,最终成就了一个名为:结构之法 算法之道的编程面试与算法研究并重的博客,如今,此博客影响力逐步渗透到海外,及至到整个互联网。 在此之前,由于本人笨拙,这微软面试100题的案只整理到了前60题(第1-60题案可到本人资源下 载处下载:http://v_july_v.download.csdn.net/),故此,常有朋友留言或来信询问后面40题的案。只是 因个人认为:一、案只是作为一个参考,不可太过依赖;、常常因一些事情耽搁(如在整理最新的今年 九月、十月份的面试题:九月腾讯,创新工场,淘宝等公司最新面试十三题、十月百度,阿里巴巴,迅雷搜狗 最新面试十一题);三、个人正在针对那100题一题一题的写文章,多种思路,不断优化,即成程序员编程 艺术系列。自此,后面40题的案迟迟未得整理。且个人已经整理的前60题的案,在我看来,是有诸多问 题与弊端的,甚至很多案都是错误的。 互联网总是能给人带来惊喜。前几日,一位现居美国加州的名叫阿财的朋友发来一封邮件,并把他自己 做的全部100题的案一并发予给我,自此,便似遇见了知己。十分感谢。 任何东西只有分享出来才更显其价值。本只需贴出后面40题的案,因为前60题的案本人早已整理上 传至网上,但多一种思路多一种参考亦未尝不可。特此,把阿财的案再稍加整理番,然后把全部100题的 案现今都贴出来。若有任何问题,欢迎不吝指正。谢谢。 上千上万的人都关注过此100题,且大都都各自贡献了自己的思路,或回复于微软100题维护地址上,或 回复于本博客内,人数众多,无法一一标明,特此向他们诸位表示敬意和感谢。谢谢大家,诸君的努力足以影 响整个互联网,咱们已经迎来一个分享互利的新时代。 感谢诸君,请享用.....
作者:July、阿财。 时间:零一一年十月十三日。 ------------------------------ 无私分享造就开源的辉煌。 今是零一一年十月十三日,明日14日即是本人刚好开博一周年。在一周年之际,特此分享出微软面试 全部100题案的完整版,以作为对本博客所有读者的回馈。 一年之前的10月14日,一个名叫July 的人在一个叫csdn 的论坛上开帖分享微软等公司数据结构+算法 面试100题,自此,与上千网友一起做,一起思考,一起解这些面试题目,最终成就了一个名为:结构之法 算法之道的编程面试与算法研究并重的博客,如今,此博客影响力逐步渗透到海外,及至到整个互联网。 在此之前,由于本人笨拙,这微软面试100题的案只整理到了前60题(第1-60题案可到本人资源下 载处下载:http://v_july_v.download.csdn.net/),故此,常有朋友留言或来信询问后面40题的案。只是 因个人认为:一、案只是作为一个参考,不可太过依赖;、常常因一些事情耽搁(如在整理最新的今年 九月、十月份的面试题:九月腾讯,创新工场,淘宝等公司最新面试十三题、十月百度,阿里巴巴,迅雷搜狗 最新面试十一题);三、个人正在针对那100题一题一题的写文章,多种思路,不断优化,即成程序员编程 艺术系列。自此,后面40题的案迟迟未得整理。且个人已经整理的前60题的案,在我看来,是有诸多问 题与弊端的,甚至很多案都是错误的。 互联网总是能给人带来惊喜。前几日,一位现居美国加州的名叫阿财的朋友发来一封邮件,并把他自己 做的全部100题的案一并发予给我,自此,便似遇见了知己。十分感谢。 任何东西只有分享出来才更显其价值。本只需贴出后面40题的案,因为前60题的案本人早已整理上 传至网上,但多一种思路多一种参考亦未尝不可。特此,把阿财的案再稍加整理番,然后把全部100题的 案现今都贴出来。若有任何问题,欢迎不吝指正。谢谢。 上千上万的人都关注过此100题,且大都都各自贡献了自己的思路,或回复于微软100题维护地址上,或 回复于本博客内,人数众多,无法一一标明,特此向他们诸位表示敬意和感谢。谢谢大家,诸君的努力足以影 响整个互联网,咱们已经迎来一个分享互利的新时代。 感谢诸君,请享用.....

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧