面试笔试题
1.
某种传染病第一天只有一个患者,前五天为潜伏期,不发作也不会传染人
第6天开始发作,从发作到治愈需要5天时间,期间每天传染3个人
求第N天共有多少患者
2.
将字符串中相邻相同的子串合并为一个子串,如"12342343454565678789" -- "123456789"
3.
求一个串中出现的第一个最长重复子串。采用顺序结构存储串,实现求串s中出现的第一个最长重复子串的下标和长度
4.
求bit位中1的总个数为n的所有整数集合
比如,二进制位中有两个1的整数为:
0x00000003
0x00000005
0x00000006
.
.
.
0xc0000000
5.
去掉整数的bit位为1的最高的两个bit位,如0x1030f --> 0x10f
6.
unsigned int intvert(unsigned int x, int p, int n)
实现对x的进行转换, p为起始转化位(最左边为第1位算起), n为需要转换的长度, 对这些bit位取反
假设起始点在右边.如x = 0b0001 0001, p=4, n=3 转换后x = 0b0110 0001
7.
C和C++混合编程,C源文件中会调用C++源文件中定义的函数int func_cpp(int),C++也会调用C源程序中定义的函数int func_c(int),
请组织程序的结构c.c, cpp.cpp, pro.h
//第一题
#include<iostream>
using namespace std;
unsigned int func(unsigned int (&array)[10], int days)
{
unsigned int new_infected = 0;//记录新增的被感染者的人数
unsigned int infect_able = 0; //记录会传染的人数
unsigned int total = 0; //当天的总或者人数
int i;
for (i = 5; i < 10; i++)
infect_able += array[i];
for (i = 0; i < days; i++)
{
new_infected = infect_able * 3;
infect_able = infect_able + array[4] - array[9];
memmove(array + 1, array, sizeof (int) * 9);
array[0] = new_infected;
}
total = infect_able;
for (i = 0; i < 5; i++)
total += array[i];
return total;
}
int main()
{
unsigned int array[10] = {1};//第一天一个病人
int days;
cout<<"输入天数:"<<endl;
cin>>days;
int total = func(array, days);
cout<<"第"<<days<<"天患者人数为:"<<total<<endl;
system("pause");
return 0;
}
//第二题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *func(char *src)
{
int all[256];
int cur_index;
if (NULL == src)
return NULL;
for (cur_index = 0; cur_index < 256; cur_index++)
{
all[cur_index] = -1;
}
cur_index = 0;
while (src[cur_index])
{
int front_index = all[src[cur_index]];
if (front_index == -1)
{
all[src[cur_index]] = cur_index;
++cur_index;
}
else
{
int rear_index = cur_index;
for (; front_index < cur_index; front_index++, rear_index++)
{
if (src[front_index] != src[rear_index])
break;
}
if (front_index != cur_index)
{
all[src[cur_index]] = cur_index;
++cur_index;
}
else
{
strcpy(src + cur_index, src + rear_index);
}
}
}
return src;
}
int main()
{
char src[1000];
puts("请输入字符串");
gets(src);
printf("%s/n", func(src));
system("pause");
return 0;
}
//第三题
#include<iostream>
#include<vector>
#include<assert.h>
using namespace std;
void func(const char *src, int *pindex, int *plength)
{
vector<int> all[256];
int cnt;
assert(NULL != src && NULL != pindex && NULL != plength);
*pindex = -1;
*plength = 0;
for (cnt = 0; src[cnt]; ++cnt)
{
int times;
for (times = 0; times < all[src[cnt]].size(); ++times)
{
int pre = all[src[cnt]][times];
int cur = cnt;
while (src[pre] == src[cur])
{
++pre;
++cur;
}
if (pre - all[src[cnt]][times] > *plength)
{
*pindex = all[src[cnt]][times];
*plength = pre - all[src[cnt]][times];
}
}
all[src[cnt]].push_back(cnt);
}
}
int main()
{
int index;
int length;
char src[1000];
puts("请输入字符串:");
gets(src);
func(src, &index, &length);
puts("结果如下:");
cout<<"index = "<<index<<endl;
cout<<"length = "<<length<<endl;
for (int i = index; i < index + length; i++)
{
cout<<src[i];
}
cout<<endl;
system("pause");
return 0;
}
//第四题
#include<stdio.h>
#include<stdlib.h>
int get_next(unsigned int *pval)
{
int cur;
int low, hig;
for (cur = 0; cur != (sizeof(*pval) << 3) - 1; ++cur)
{
if ((*pval & (1 << cur)) && !((*pval & (1 << (cur + 1)))))
break;
}
if ((sizeof(*pval) << 3) - 1 == cur)
{
return 0;
}
for (low = 0, hig = cur + 1; !(*pval & (1 << low)); ++low)
;
*pval ^= (1 << hig);
*pval ^= (1 << low);
for (hig = cur, low = 0; hig > low; --hig, ++low)
{
if (((*pval & (1 << hig)) && (!(*pval & (1 << low)))) || ((*pval & (1 << low)) && (!(*pval & (1 << hig)))))
{
*pval ^= (1 << hig);
*pval ^= (1 << low);
}
}
return 1;
}
int main()
{
unsigned int val;
int bit1_num;
printf("输入二进制位中1的个数:/n");
scanf("%d", &bit1_num);
if ((sizeof(int) << 3) < bit1_num || bit1_num < 0)
{
printf("Error/n");
exit(1);
}
if ((sizeof(unsigned int) << 3) == bit1_num)
val = -1;
else
val = ((1 << bit1_num) - 1);
do
{
printf("%x/n", val);
}while (get_next(&val));
system("pause");
return 0;
}
//第五题
#include<stdio.h>
#include<stdlib.h>
unsigned int func(unsigned int val)
{
unsigned int ui = val;
unsigned int left = 0, right = 0;
while (ui)
{
right = left;
left = (ui & -ui);
ui &= (ui - 1);
}
return (val & ~(left | right));
}
int main(int argc,char* argv[])
{
printf("%x/n", func(0x1030f));
system("pause");
return 0;
}
//第六题
#include<iostream>
using namespace std;
inline unsigned int intvert(unsigned int x, int p, int n)
{
//这里加上参数的有效性检查!
return x ^ (((1 << n) - 1) << (p - 1));
}
int main()
{
unsigned int x = 0;
printf("%x/n", intvert(x, 4, 3));
system("pause");
return 0;
}
//第七题
//pro.h
#ifndef _PRO_H_
#define _PRO_H_
#ifdef __cplusplus
extern "C"
{
#endif //__cplusplus
int func_cpp(int);
int func_c(int);
#ifdef __cplusplus
}
#endif //__cplusplus
#endif //_PRO_H_
//c.c
#include"pro.h"
int func_c(int)
{
...
}
//cpp.cpp
#include"pro.h"
int main()
{
...
}
int func_cpp(int)
{
...
}