微软应聘试题算法求解

dongle2001 2004-09-02 01:53:38
某日一友到微软应聘笔试,遇到这样一题,拿与各位分享:

有4个人过桥,这4个人单独过桥的时间分别为1分钟、2分钟、5分钟、10分钟;他们只有一个手电筒,一次过桥只能同时过两个人,速度按慢的人算,然后其中一个人再拿手电筒回来接其他的人再过去;请问他们按怎样的顺序过桥所花的时间最少?

最少时间是多少?


...全文
265 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongle2001 2004-09-19
  • 打赏
  • 举报
回复
写代码没有注释让我怎么看?
showjancn 2004-09-19
  • 打赏
  • 举报
回复
说明一下!!
要算法,不是要代码。
kugou123 2004-09-19
  • 打赏
  • 举报
回复
TO Paris_Luo(不懂):
弓虽!!!
双杯献酒 2004-09-19
  • 打赏
  • 举报
回复
UP
lionhart 2004-09-19
  • 打赏
  • 举报
回复
这个是ACM/ICPC里的一道练习题.
Paris_Luo 2004-09-16
  • 打赏
  • 举报
回复
花了我2小时 :(

#include <iostream.h>
#include <vector>
using namespace std;

#define max(a,b) (a>b?a:b)

typedef struct treeNode{
unsigned int time;
vector<int> start,end;
treeNode *firstChild,*nextSibling;
bool result;
}TreeNode,*pTreeNode;

void moveStart2End(vector<int> start, vector<int> end, int num1,int num2, vector<int> &nextStart, vector<int> &nextEnd)
{
nextStart.assign(start.begin(),start.end());
nextEnd.assign(end.begin(),end.end());
nextEnd.push_back(nextStart[num1]);
nextEnd.push_back(nextStart[num2]);
nextStart.erase(nextStart.begin()+num1);
nextStart.erase(nextStart.begin()+num2-1);
}

void moveEnd2Start(vector<int> start, vector<int> end, int num, vector<int> &nextStart, vector<int> &nextEnd)
{
nextStart.assign(start.begin(),start.end());
nextEnd.assign(end.begin(),end.end());
nextStart.push_back(nextEnd[num]);
nextEnd.erase(nextEnd.begin()+num);
}

void go(pTreeNode &first)
{
pTreeNode preSibling = NULL;
pTreeNode nextFirst;
while(first != NULL){
for(int i=0; i<(first->start).size()-1; i++)
for(int j=i+1; j<(first->start).size(); j++){
pTreeNode child = new TreeNode;
moveStart2End(first->start,first->end,i,j,child->start,child->end);
child->time = max((first->start)[i],(first->start)[j]) + first->time;
child->firstChild = NULL;
child->nextSibling = NULL;
if( (child->start).size() == 0)
child->result = true;
else
child->result = false;
if(preSibling == NULL) {
first->firstChild = child;
nextFirst = child;
preSibling = child;
}
else{
preSibling->nextSibling = child;
preSibling = child;
}
}
first = first->nextSibling;
}
first = nextFirst;
}

void back(pTreeNode &first)
{
pTreeNode preSibling = NULL;
pTreeNode nextFirst;
while(first != NULL){
for(int i=0; i<(first->end).size(); i++){
pTreeNode child = new TreeNode;
moveEnd2Start(first->start,first->end,i,child->start,child->end);
child->time = (first->end)[i] + first->time;
child->firstChild = NULL;
child->nextSibling = NULL;
child->result = false;
if(preSibling == NULL) {
first->firstChild = child;
nextFirst = child;
preSibling = child;
}
else{
preSibling->nextSibling = child;
preSibling = child;
}
}
first = first->nextSibling;
}
first = nextFirst;
}

void midTrasverseTree(pTreeNode root,vector<int> &result)
{
pTreeNode oldRoot = root;
while(root){
if(root->result)
result.push_back(root->time);
root = root->nextSibling;
}
if(oldRoot->firstChild !=NULL )
midTrasverseTree(oldRoot->firstChild,result);
}

int get_smallest(vector<int> result)
{
int ret = result[0];
for(int i=1;i<result.size();i++){
if(ret > result[i])
ret = result[i];
}
return ret;
}

void main()
{
vector<int> result;
int smallest;
pTreeNode root = new TreeNode;
root->time = 0;
(root->start).push_back(1);
(root->start).push_back(2);
(root->start).push_back(5);
(root->start).push_back(10);
root->firstChild = NULL;
root->nextSibling = NULL;
root->result = false;
pTreeNode oldRoot = root;
for(int i=0;i<(root->start).size();i++){
go(root);
back(root);
}
go(root);
midTrasverseTree(oldRoot,result);
smallest = get_smallest(result);
cout << "answer is:" << smallest << endl;
}
bestbear 2004-09-02
  • 打赏
  • 举报
回复
俺找工作那会在好几家公司做过
第一次觉得新鲜,越做越没劲
  • 打赏
  • 举报
回复
先排序,再计算,不应该比较,
hapybird 2004-09-02
  • 打赏
  • 举报
回复
你写一个程序,二纬数组,把每种情况作为一个数组,每对作为一个子数组,全部算一下就知道了
Paris_Luo 2004-09-02
  • 打赏
  • 举报
回复
mark
Pipi0714 2004-09-02
  • 打赏
  • 举报
回复
好像看过这样的题:
1,2 ==>
<== 1
5,10 ==>
<== 2
1,2 ==>

17分钟
dongle2001 2004-09-02
  • 打赏
  • 举报
回复
请各位给出算法
  • 打赏
  • 举报
回复
2,1 ==> 1 <== 2+1
10,5 ==> 2 <== 10+2
2,1 ==> 2


=17
dafei81 2004-09-02
  • 打赏
  • 举报
回复
17就够了
  • 打赏
  • 举报
回复
10,1 ==> 1 <== 10+1
5,1 ==> 1 <== 5+1
2,1 ==> 2

= 19
悟空大王 2004-09-02
  • 打赏
  • 举报
回复
狗屁,这样得问题在实际应用中实用么
  • 打赏
  • 举报
回复
10,5==> 5 <== 10+5
5,2 ==> 2 <== 5+2
2,1==> 2

= 24

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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