某日一友到微软应聘笔试,遇到这样一题,拿与各位分享: 有4个人过桥,这4个人单独过桥的时间分别为1分钟、2分钟、5分钟、10分钟;他们只有一个手电筒,一次过桥只能同时过两个人,速度按慢的人算,然后其中一个人再拿手电筒回来接其他的人再过去;请问他们按怎样的顺序过桥所花的时间最少? 最少时间是多少?
解决方案 »
- 通过ODBC读取Excel文件内容,出现一个小问题,特来请教……
- 请问在linux中,从驱动如何发信号到应用程序的信号接收函数?100分,谢谢!
- 执行错误:Unhandled exception
- 怎样用CFile的Write写入一个换行付,谢了,问题还是简单的
- 用vc6.0怎么把写完的程序做成安装文件呢??那个installShieled是什么东西啊??
- dll和activex以及线程
- 求两篇rfc的中文文档
- 请高手明示:发明语言的人一定是自己写出第一个编译器么?
- 如何得到程序窗口外的鼠标事件
- 请推荐几本Visual C++的入门好书(我是指VC++这个工具,不是指C++语言)
- DataGrid的问题!!!!!!!!
- 如何获得局域网内所有的电脑名称? 急在线等~~
5,2 ==> 2 <== 5+2
2,1==> 2= 24
5,1 ==> 1 <== 5+1
2,1 ==> 2= 19
10,5 ==> 2 <== 10+2
2,1 ==> 2
=17
1,2 ==>
<== 1
5,10 ==>
<== 2
1,2 ==>17分钟
#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;
}
弓虽!!!