网摘一道百度2014年9月25日面试题(题目是网上看到的,代码是自己写的)
2014-09-27 12:21
645 查看
题目:一个char数组只包含a,b,c,d,e五种字符,设计一种算法,找出一个包含五种字符的最小区间【a,b】,数组是循环的(比如区间[9,2]也是可以的).
思路:找到所有含有abcde这五个字符的区间,取最小区间并且记录最小区间的下标。每找到一个含有abcde的区间就记录下,然后把计数数组清0,为了方便下一轮的统计区间长度。
代码如下:
#include <iostream>
#include <time.h>
using namespace std;
const n=10;
char f(int x)
{
char temp='\0';
switch(x)
{
case 1:temp='a';
break;
case 2:temp='b';
break;
case 3:temp='c';
break;
case 4:temp='d';
break;
case 5:temp='e';
break;
}
return temp;
}
void main()
{
srand(unsigned int (time(NULL)));
char a
={0};
int count[5]={0},Min=n,j=0,i=0,flag=0,start=0,end=0;
for (i=0;i<n;i++)
{
int x=rand()%5+1;
a[i]=f(x);
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"所有包含所有abcde五个字符的区间为"<<endl;
i=0;
while(j!=n)
{
if (i==n)
{
if (j==0)
{
cout<<"该字符数组内缺少字符!"<<endl;
break;
}
else
{
i=0;
}
}
if (a[i]=='a')
{
count[0]++;
}
else if(a[i]=='b')
{
count[1]++;
}
else if (a[i]=='c')
{
count[2]++;
}
else if(a[i]=='d')
{
count[3]++;
}
else
{
count[4]++;
}
if (count[0]*count[1]*count[2]*count[3]*count[4]!=0)
{
cout<<"["<<j<<","<<i<<"]"<<" ";
if (Min>count[0]+count[1]+count[2]+count[3]+count[4])
{
Min=count[0]+count[1]+count[2]+count[3]+count[4];
start=j;
end=i;
}
count[0]=count[1]=count[2]=count[3]=count[4]=0;
i=j;
j++;
}
i++;
}
cout<<endl;
cout<<"最小区间=["<<start<<","<<end<<"]"<<endl;
}
题目:一个char数组只包含a,b,c,d,e五种字符,设计一种算法,找出一个包含五种字符的最小区间【a,b】,数组是循环的(比如区间[9,2]也是可以的).
思路:找到所有含有abcde这五个字符的区间,取最小区间并且记录最小区间的下标。每找到一个含有abcde的区间就记录下,然后把计数数组清0,为了方便下一轮的统计区间长度。
代码如下:
#include <iostream>
#include <time.h>
using namespace std;
const n=10;
char f(int x)
{
char temp='\0';
switch(x)
{
case 1:temp='a';
break;
case 2:temp='b';
break;
case 3:temp='c';
break;
case 4:temp='d';
break;
case 5:temp='e';
break;
}
return temp;
}
void main()
{
srand(unsigned int (time(NULL)));
char a
={0};
int count[5]={0},Min=n,j=0,i=0,flag=0,start=0,end=0;
for (i=0;i<n;i++)
{
int x=rand()%5+1;
a[i]=f(x);
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"所有包含所有abcde五个字符的区间为"<<endl;
i=0;
while(j!=n)
{
if (i==n)
{
if (j==0)
{
cout<<"该字符数组内缺少字符!"<<endl;
break;
}
else
{
i=0;
}
}
if (a[i]=='a')
{
count[0]++;
}
else if(a[i]=='b')
{
count[1]++;
}
else if (a[i]=='c')
{
count[2]++;
}
else if(a[i]=='d')
{
count[3]++;
}
else
{
count[4]++;
}
if (count[0]*count[1]*count[2]*count[3]*count[4]!=0)
{
cout<<"["<<j<<","<<i<<"]"<<" ";
if (Min>count[0]+count[1]+count[2]+count[3]+count[4])
{
Min=count[0]+count[1]+count[2]+count[3]+count[4];
start=j;
end=i;
}
count[0]=count[1]=count[2]=count[3]=count[4]=0;
i=j;
j++;
}
i++;
}
cout<<endl;
cout<<"最小区间=["<<start<<","<<end<<"]"<<endl;
}
相关文章推荐
- 【面试题】从网上看到的一道面试题之自我见解
- 网上看到的一道腾讯面试题
- JS代码大全 (都是网上看到 自己整理的)
- 今天在网上看到一个百度的C++面试题目
- 自己在书上看到的 一道简单但很有意思的题目
- 一道网上看到的面试题(做着玩 用到递归了)
- 矩阵相乘 指针指针 网上看到的一道题目
- JS代码大全(都是网上看到自己整理的)
- Mr.Cao 提出的百度面试题,以及自己从网上找的答案
- JS代码大全 (都是网上看到 自己整理的)
- 【题目36】百度的一道面试题-找到攻击IP
- 经常看到有关猫,老鼠,主人的那个题目,这里我想说一下自己的一点想法
- 在网上看到一篇文章 安慰一下自己孤寂的心灵
- 在CSDN上看到的一道有趣的网易笔试题目--附答案
- 百度笔试题2005题目大致是这样的: 第一部分选择题: 有几道网络相关的题目,巨简单,比如第一题是TCP、RIP、IP、FTP中哪个协议是传输层的......。有一道linux的 chown使用题目。其他的全是数据结构的题目!什么链,表
- 百度面试题一道
- 百度很有意思的一道面试题目
- 一道百度面试题题解
- (网上看到的,觉得不错,转载了)你想不想使自己的收入加倍
- 如果在高速公路上30分钟内到一辆车开过的几率是0.95,那么在10分钟内看到一辆车开过的几率是多少 (假设为常概率条件下) - Google, 谷歌,百度,baidu,阿里巴巴,alibaba,微软,华为,huawei面试题,