三道面试题,大家看看有什么思路。

jasonhrs 2004-12-04 10:43:03
3题两小时,2条编程,一条逻辑题

1、给一个字符串、例如 “ababc”要求返回“ab”
因为“ab”连续重复出现且最长。

用C/C++语言写一函数完成该算法,给出复杂度

2、对序列1、1、2、3、5、8、13。。。。 是Fab..数列
2、3、5、13...是Fab..质数数列,因为他们与自己前面的Fab...数列都互质

给出k,返回第k小的Fab..质数

3、101个硬币100真、1假,真假区别在于重量。请用无砝码天平称两次给出
真币重还是假币重的结论。
...全文
1303 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
林初茵 2005-08-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;





char * fun(char * str)
{
int i=0,j=1,k=1,len=1,sublen,flag,flag1,flag2,ii,tolen,max=0;

char *q,*p,*pp,*pp2;

p=str;
q=str;
len=1;
sublen=strlen(p)/2;
tolen=strlen(p);
// maxpp[0]='s';
char *maxpp = (char*)malloc(10);


while(i<tolen)
{ //111111111111111111
flag=i;
j=i+1;
while(j<tolen)
{
if(q[i]==p[j])
{

flag1=i;
flag2=j;
k=0;
// y=y+1;
while(i<flag2&&j<tolen)
{
if(q[i]!=p[j])
break;
else
{
pp[k]=p[i];
i++;
j++;
k++;
}
pp[k]=0;
}
//cout << "ttt"<<pp << endl;
//cout << strlen(pp) << endl;
if(strlen(pp)>max)
{
cout << "max"<<max << endl;
max=strlen(pp);
strcpy(maxpp,pp);
// maxpp[max]=0;
cout << "yyy"<< maxpp << endl;
//cout << "yyy"<< strlen(maxpp) << endl;
}

i=flag1;
j=flag2;
}

j++;

//cout << "zzz2"<< maxpp << endl;
//if(strlen(maxpp)<3)
//{cout << "ggg"<< maxpp << endl;}
}



i=flag+1;
}

//cout << "uuu"<< maxpp << endl;
//cout << "yyy"<< strlen(maxpp) << endl;
return(maxpp);

//11111111111111111111
}

int main()
{
// char * tmp = "cabaca";
char * tmp2 = "zabcdabcdza";
// char * str = GetRepeatLongestSuhStr(tmp);
char * str2 = fun(tmp2);
//cout << str << endl;
cout << str2 << endl;
system("pause");

return 0;
}
ten91 2004-12-06
  • 打赏
  • 举报
回复
能指点指点,从学****到学****,才成一名合格的程序员???
imRainman 2004-12-06
  • 打赏
  • 举报
回复
第一题比较简单做法;

#include <stdio.h>
#include <string.h>

void find(const char *str)
{
int len = (int)strlen(str) ;
char *result = new char[len - 1] ;

for (int i = len / 2 ; i >= 1 ; i--)
{
for (int x = 0 ; x < len - i ; x++)
result[x] = (str[x + i] == str[x]) ;
for (int x = 0, c = 0 ; x < len - i ; x++)
{
if (result[x]) c++ ; else c = 0 ;
if (c == i)
{
for (int y = 0 ; y < i ; y++)
printf("%c", str[x - i + y + 1]) ;
delete[ ] result ;
return ;
}
}
}
delete[ ] result ;
printf("No match!") ;
}

void main( )
{
find("ababc") ;
}
jasonhrs 2004-12-05
  • 打赏
  • 举报
回复
ft,从devcpp里考出来是乱码!
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

char * GetRepeatLongestSuhStr(char * str)
{
int totalLen;
//取得整个字符串的长度。
totalLen = strlen(str);
cout << "total len = " << totalLen << endl;
//从左到右扫描到的索引值.
int index;
//用来保存临时的连续重复出现的字符串.
char * tmpstr;
int startIndex = 0;
//保存最长的连续重复出现的字符串.
int maxlen = 0;
char *maxstr;
/*
先从最左的开始,一个一个字符地扫描,设扫到的index为index.
在扫描到的字符及其右边的所有字符里查找最长的连续出现的字符串.
具体方法是:
1.取得这些字符串的长度,除以2,得到可能的重复字符串的最长长度.
2. 判断substr(index,index+len)和substr(index+len,index+len+len)是不是一样,是的话返回substr(index,index+len);
3.不是则len-1,重复第2步直到len = 0;
4.index 加一,重复第1步.
例如有"cababc",则:
totalLen = 6;index = 0;
现在从左开始
当index = 0 时,在"cababc"里找,len = totalLen/2 = 3;
1.cab != abc len --;
2.ca != ba len --;
3.c != a len --;
index ++; 则现在在"ababc"里查找
1.ab == ab 返回"ab",保存在tmpstr和maxstr里
index ++ ;则现在在"baba"里查找
...
...
找不到...
若是以后查到也有连续出现的字符串,则先保存在tmpstr里,然后长度与maxstr的长度比较,若是长度大于maxstr,则maxstr = tmpstr;
返回maxstr即可.
具体程序没写完,我慢慢写,你先理解一下.
*/
for(index = 0 ;index < totalLen; index ++)
{
int sublen = (totalLen - index)/2;
cout << "sublen = " << sublen << endl;
for( ;sublen > 0 ;sublen --)
{
int tmp = 0;
while(str[index + tmp] == str[index+sublen+tmp])
tmp++;
if(tmp > 0)
if(tmp > maxlen)
{
maxlen = tmp;
startIndex = index;
}
}
}
if(maxlen > 0 )
{
maxstr = new char[maxlen+1];
for(int i = 0 ;i < maxlen ;i ++)
maxstr[i] = str[startIndex+i];
maxstr[maxlen] = '\0';
return maxstr;
}
return NULL;
}

void main()
{
char * tmp = "abcabcabc";
char * str = GetRepeatLongestSuhStr(tmp);
cout << str << endl;
system("pause");
}

第二题:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int* fab = new int[1024];
int* prime = new int[2048];
int getNum(int k)
{

fab[0] = fab[1] = 1;
prime[0] = 2;
int j = 2;
for(int i = 1;i < k ;)
{
int tmp = fab[j-1] + fab[j -2];
fab[j++] = tmp;
int m = 0;
for(; m < i ; m ++)
{
if(tmp % prime[m] == 0)
break;
}
if( m == i )
prime[i++] = tmp;
}
return prime[k -1];
}

void main()
{
int tmp = getNum(20);
int i;
for(i = 1 ;i < 50 ;i ++)
cout << fab[i] << " ";
cout << endl;
for(i = 0 ;i < 19;i ++)
cout << prime[i] << " ";
cout << endl;
system("pause");
}
jasonhrs 2004-12-05
  • 打赏
  • 举报
回复
本来是同学在广州要我帮忙看的,我看时间不够就发到上面来了,后来自己写着看还是很容易的,用不了半个小时。
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

char * GetRepeatLongestSuhStr(char * str)
{
int totalLen;
//È¡µÃÕû¸ö×Ö·û´®µÄ³¤¶È¡£
totalLen = strlen(str);
cout << "total len = " << totalLen << endl;
//´Ó×óµ½ÓÒɨÃèµ½µÄË÷ÒýÖµ.
int index;
//ÓÃÀ´±£´æÁÙʱµÄÁ¬ÐøÖظ´³öÏÖµÄ×Ö·û´®.
char * tmpstr;
int startIndex = 0;
//±£´æ×µÄÁ¬ÐøÖظ´³öÏÖµÄ×Ö·û´®.
int maxlen = 0;
char *maxstr;
/*
ÏÈ´Ó×î×óµÄ¿ªÊ¼,Ò»¸öÒ»¸ö×Ö·ûµØɨÃè,Éèɨµ½µÄindexΪindex.
ÔÚɨÃèµ½µÄ×Ö·û¼°ÆäÓұߵÄËùÓÐ×Ö·ûÀï²éÕÒ×µÄÁ¬Ðø³öÏÖµÄ×Ö·û´®.
¾ßÌå·½·¨ÊÇ:
1.È¡µÃÕâЩ×Ö·û´®µÄ³¤¶È,³ýÒÔ2,µÃµ½¿ÉÄܵÄÖظ´×Ö·û´®µÄ×³¤¶È.
2. ÅжÏsubstr(index,index+len)ºÍsubstr(index+len,index+len+len)ÊDz»ÊÇÒ»Ñù,ÊǵĻ°·µ»Øsubstr(index,index+len);
3.²»ÊÇÔòlen-1,Öظ´µÚ2²½Ö±µ½len = 0;
4.index ¼ÓÒ»,Öظ´µÚ1²½.
ÀýÈçÓÐ"cababc",Ôò:
totalLen = 6;index = 0;
ÏÖÔÚ´Ó×ó¿ªÊ¼
µ±index = 0 ʱ,ÔÚ"cababc"ÀïÕÒ,len = totalLen/2 = 3;
1.cab != abc len --;
2.ca != ba len --;
3.c != a len --;
index ++; ÔòÏÖÔÚÔÚ"ababc"Àï²éÕÒ
1.ab == ab ·µ»Ø"ab",±£´æÔÚtmpstrºÍmaxstrÀï
index ++ ;ÔòÏÖÔÚÔÚ"baba"Àï²éÕÒ
...
...
ÕÒ²»µ½...
ÈôÊÇÒÔºó²éµ½Ò²ÓÐÁ¬Ðø³öÏÖµÄ×Ö·û´®,ÔòÏȱ£´æÔÚtmpstrÀï,È»ºó³¤¶ÈÓëmaxstrµÄ³¤¶È±È½Ï,ÈôÊdz¤¶È´óÓÚmaxstr,Ôòmaxstr = tmpstr;
·µ»Ømaxstr¼´¿É.
¾ßÌå³ÌÐòûдÍê,ÎÒÂýÂýд,ÄãÏÈÀí½âÒ»ÏÂ.
*/
for(index = 0 ;index < totalLen; index ++)
{
int sublen = (totalLen - index)/2;
cout << "sublen = " << sublen << endl;
for( ;sublen > 0 ;sublen --)
{
int tmp = 0;
while(str[index + tmp] == str[index+sublen+tmp])
tmp++;
if(tmp > 0)
if(tmp > maxlen)
{
maxlen = tmp;
startIndex = index;
}
}
}
if(maxlen > 0 )
{
maxstr = new char[maxlen+1];
for(int i = 0 ;i < maxlen ;i ++)
maxstr[i] = str[startIndex+i];
maxstr[maxlen] = '\0';
return maxstr;
}
return NULL;
}

void main()
{
char * tmp = "abcabcabc";
char * str = GetRepeatLongestSuhStr(tmp);
cout << str << endl;
system("pause");
}

第二题:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int* fab = new int[1024];
int* prime = new int[2048];
int getNum(int k)
{

fab[0] = fab[1] = 1;
prime[0] = 2;
int j = 2;
for(int i = 1;i < k ;)
{
int tmp = fab[j-1] + fab[j -2];
fab[j++] = tmp;
int m = 0;
for(; m < i ; m ++)
{
if(tmp % prime[m] == 0)
break;
}
if( m == i )
prime[i++] = tmp;
}
return prime[k -1];
}

void main()
{
int tmp = getNum(20);
int i;
for(i = 1 ;i < 50 ;i ++)
cout << fab[i] << " ";
cout << endl;
for(i = 0 ;i < 19;i ++)
cout << prime[i] << " ";
cout << endl;
system("pause");
}
jasonhrs 2004-12-05
  • 打赏
  • 举报
回复
这是今天网易在广州的笔试题,不好意思了,是笔试题。
DuoFG 2004-12-05
  • 打赏
  • 举报
回复
做出来并不困难
但要想在短时间内做出来还是需要基本功的。
jasonhrs 2004-12-05
  • 打赏
  • 举报
回复
ft
san_126 2004-12-05
  • 打赏
  • 举报
回复
面试题就这么狡猾,呵呵,笔试还得了啊
敢问兄台面试哪个公司啊?
clm1457 2004-12-05
  • 打赏
  • 举报
回复
A B C D
33 33 34 1


if (B+D==C) B C D 为真,在A中
A>B // 假>真
A<B // 真>假

if(B+D>C) A为真
A+D>C 假>真 在A中
A+D<C //不可能存在
A+D==C 真>假(A>B) A为真 在B中


if(B+D<C) A为真
A+D>C //不可能存在
A+D<C 假>真 在A中
A+D==C 真>假(A>B) A为真 在B中

小弟想了大半个小时
有一点头绪,太烦不想了。
把自己的一点想法贴上,如有误还请指教.
loveljh712 2004-12-05
  • 打赏
  • 举报
回复
yiyang up
木客 2004-12-05
  • 打赏
  • 举报
回复
guanzhu
wolf_syt 2004-12-05
  • 打赏
  • 举报
回复
第三题:
分为50+50+1,分别记做ma,mb,和1
将ma和mb放在天平两端
如果ma=mb,那么省下一枚是假的,在ma中拿出一枚,把假的放上,如果ma<mb,就是假的轻,否则假的重.
如果ma!=mb,那么省下的是真的,取ma、mb中重的一份,等分成两份再分别放在天平两端,如果重量相同,就是真的重,否则假的重。
carbonic 2004-12-05
  • 打赏
  • 举报
回复
第三题:
分为50+50+1,分别记做ma,mb,和1
如果ma=mb,那么随机取出任意的一枚,可以比较后得出结果;
如果ma<mb,我们不妨设定假币是较轻的,那么,我们可以把mb均分为两份,分别是25枚(分别记做mb1和mb2)。如果mb1=mb2,那么我们的假设就是为真,否则,我们假设错误,假币就是较重的了。^_^
dreamlife 2004-12-05
  • 打赏
  • 举报
回复
学习学习
aaasng 2004-12-05
  • 打赏
  • 举报
回复
第3题也可以拿A(50),B(50)比一下,一样的话拿剩下的一个和真的比一下。
如果不一样,就拿其中的一堆。比如A(50)再分成两堆25比一下,一样的话就在
B(50)中,不一样就在A(50)中,结合第一次的结果就知道了。
260005065 2004-12-05
  • 打赏
  • 举报
回复
1、
char* fun(char* str)
{
int i,j,k,m=0;
char *q,*p;;
while(*str){
for(i=1,k=0,p=str+1;*p;p++,i++){
if(*p==*str){
for(j=0;j<i;j++){
if(p[j]!=str[j]){
break;
}
}
if(j==i){
k=i;
if(k>m){
q=str;
m=k;
}
}
}
}
str++;
}
q[m]=0;
return q;
}
2、
int getNum(int k)
{
int m=1,n=1,i;
while(k){
m+=n;
for(i=2;m%i;i++);
if(m==i){
k--;
}
i=m,m=n,n=i;
}
return n;
}
adorph 2004-12-05
  • 打赏
  • 举报
回复
Sorry,因找不到"小类社区"发新帖,只好接在您后面啦^_^
--------------------------------------------------
本人因学习需要:
1. "Visual Basic 6.0 中文版程序员指南"
Microsoft Corporation 著
微软(中国)有限公司 译
北京希望电脑公司 出品
(含一张配套光盘)

2. "Microsoft Visual C++ 6.0 程序员指南"
Beck Zaratian 著
微软授权出版
(含一张配套光盘)

3. (中文版, 英文版皆可) (含一张配套光盘)
中文名: Programming Windows程式开发设计指南(第五版)
作者: Charles Petzold
英文名: Programming Windows by Charles Petzold (Fifth Edition)
Microsoft Press

交易地点: 限南京市
本人: 孙先生
本人联系方式:
Email: jerysun0818@hotmailcom
或 jerysun0818@yahoo.com.cn
手机: 13003424733 (请发短信, 本人囊中羞涩, 从不接手机:-)

您的回复信息格式为:
1. 您的姓名, 联系方式(手机, Email)
2. 以上您有哪几本还是全有? 您出的交易价格?
3. 在何时, 何地交易?
lkforever 2004-12-05
  • 打赏
  • 举报
回复
第三题简单
101个先取出2堆,
33,33
第一次称,如果不相等,说明有一堆重或轻
那么把重的那堆拿下来,再放另外35个中的33
如果相等,说明假的重,如果不相等,新放上去的还是重的话,说明假的轻(不可能新放上去的轻)

第一次称,如果相等的话,这66个肯定都是真的,从这66个中取出35个来,与剩下的没称过的35个比
下面就不用说了
myc 2004-12-05
  • 打赏
  • 举报
回复
mark
加载更多回复(1)

64,648

社区成员

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

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