Chinaunix首页 | 论坛 | 博客
  • 博客访问: 379269
  • 博文数量: 715
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:46
文章分类

全部博文(715)

文章存档

2011年(1)

2008年(714)

我的朋友

分类:

2008-10-13 16:35:47

FIBo:我觉得算法考虑的因素不够。
面试的原意好象是给出个序号,然后从这个序号开始依次吃掉老鼠。那么如何快速定位到链表相应的序号呢?
也许可以这样,假设给定序号nNumber(nNumber < 已有老鼠数);
p = head;

for (int i = 1; i < nNumber; i++)
{
     p = p->next;
}
//p就是给定序号对应的数据项。

用这个方法定位,你会发现它的速度完全取决于给定的nNumber,当nNumber很大的时候会很不划算。在这一点上显然不如数组定位要快。
(发表于2004-3-18 10:27:00)

dyq:看了前面几个程序,感觉虽然代码不长,但逻辑太麻烦,语义不够直观;
我编代码的经验也不多,但感觉自己写的东西很简单,而且上机结果也没问题.
#include "stdio.h"
#include "iostream.h"

class mouse
{
public:
int num;
mouse *pre;
mouse *next;
};

void main()
{
mouse *pCurrent;//当前要吃的老鼠

//读入
int n,start;
cout<<"totally How many mice:";
cin>>n;
cout<<"Eat from which?";
cin>>start;

//初始化
mouse *pMousePre;
for(int i = 1; i <= n ; i++)
{
mouse *pMouse = new mouse;
if(i == start)
pCurrent = pMouse;
if(i != 1)
{
pMouse->pre = pMousePre;
pMousePre->next = pMouse;
}

pMouse->num = i;

if( i == n )
{
pMouse->next = pCurrent;
pCurrent->pre = pMouse;
}
pMousePre = pMouse;
}
//吃
do
{
//下一个要吃的老鼠
mouse *pEat = pCurrent;
pCurrent = pCurrent->next->next;
//吃掉本老鼠
pEat->pre->next = pEat->next;

}while(pCurrent != pCurrent->next->next);

cout<<"the last is:"<next->num;
}

(发表于2004-7-9 15:11:00)

qinhongxian:上面给出的第一个程序在最后有错误:
  for(i=0;i  {
      p=q->next;
      q=p;
      p=q->next;
      q->next=p->next;
      free(p);
   }
不能根据n来控制循环





(发表于2004-7-21 17:59:00)

竹叶:效率有点低。时间复杂度大,但是感觉挺简单的。用数组实现的,我认为链表还是数组是看怎么控制的。#include 
#define N 10
int serch(int start)
{
int sun[N] = {0};
int total = N;
int count = 0;
for(;total-- > 0;)
sun[total] = total+1;
int ok = 0;
int on = 0;
for(int i = 0; i < N; i++)
{
for(int j = (on++ == 0 ? start:0); j < N; j++)
{
if(ok == 0)
{
ok++;
sun[j] = 0;
}
if(sun[j] != 0)
{
if(count == 1)
{
ok++;
if(ok == N )
{
for(i = 0;i < N ;i++ )
printf("%d ",sun[i]);
return j;
}
sun[j] = 0;
count--;
}
else
count++;
}
}
}
}
int main()
{
printf("\n最后一个是编号为:%d的。\n",serch(2)+1);
return 1;
}
(发表于2004-8-5 12:45:00)

笨笨笑:http://blog.vckbase.com/smileonce/archive/2004/11/09/1390.aspx
(发表于2004-11-24 16:39:00)

yunteng:#include
#include
#include

typedef struct list
{
int number;
struct list *next;
}*listlink;

listlink Init(int mouse)
{
int mousenum =mouse;
int num=1;
listlink head = new list;
head->number =1;
head->next = head;
listlink p= head;
for(int i=1;i {
listlink q=new list;
q->number=i+1;
p->next =q;
q->next=head;
p=p->next;
}
 return head;

}

listlink catpositionbefore(int mouse,listlink head)
{
srand((unsigned)time(NULL));
int mousenum =mouse;
mousenum =rand()%mousenum;
// cout<<"rand()="< listlink pcat =head;
if(mousenum)
{
for(int i=1;i {
pcat =pcat->next;
}
}else
{
for(int i=1;i {
pcat =pcat->next;
}
}
cout<<"the position of cat is: "<next->number< return pcat;
}


(发表于2007-2-10 9:12:00)

yunteng:void main()
{
cout<<"Please input the number of mouse(exit to 'q'):";
int mousenum=0;;
while(cin>>mousenum)
{
if(mousenum<1)
break;
 listlink pmousehead = Init(mousenum);
 listlink head =pmousehead;
 listlink pmouse = catpositionbefore(mousenum,pmousehead);
 while(--mousenum)
 {
pmouse->next=pmouse->next->next;
pmouse=pmouse->next;
head=pmouse;
// cout<<"current mouse position ="<number<  }
cout<<"only mouse number:"<number< cout<<"Please input the number of mouse again(exit to 'q'):";

}
}
(发表于2007-2-10 9:12:00)

..........................................................................
--------------------next---------------------

阅读(156) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~