华为笔试面试题
面试题:n个人围成一圈,编号从1到n,报数1,2,3,从编号为1的人开始报数,报到3的离开,下面的人接着从1开始报数,依次下去,写程序求最后剩下来的一个人编号是几。
1.
#include <stdlib.h>
#include <stdio.h>
struct node_t
{
int serial;
struct node_t *next;
};
int main(int argc, char *argv[])
{
int n;
int result = 0;
struct node_t *people = NULL;
while(1)
{
int i;
struct node_t *first;
printf("please input the number of people. input zero to exit.\n n = ");
scanf("%d", &n);
if(n == 0) return 0;
people = (struct node_t *)malloc(sizeof(struct node_t) * n);
for(i=0; i<n-1; i++)
{
people.serial = i + 1;
people.next = &people[i+1];
}
people.serial = i + 1;
people.next = &people[0];
i = 0;
first = &people[0];
while(1)
{
first = first->next;
first->next = first->next->next;
if(first->next == first) break;
first = first->next;
}
result = first->serial;
free(people);
printf("the remain people's number is %d\n", result);
}
return 0;
}
2.
#include <stdio.h>
#include <stdlib.h>
#define IN 1
#define OUT 0
int main(int argc, char* argv[])
{
int n = atoi(argv[1]);
int i;
printf("%d\n", n);
if (n <= 1)
{
fprintf(stderr, "people number error\n");
return 0;
}
int *people;
people = (int *) malloc(n * sizeof(int));
for(i = 0; i < n; i++)
people = IN;
int in_count = n;
int count = 0;
int cur = 0;
while (in_count != 1)
{
if (people[cur] == IN)
{
count = (count + 1) % 3;
if (count == 0)
{
people[cur] = OUT;
in_count --;
}
}
cur = (cur + 1) % n;
}
for(i = 0; i < n; i++)
{
if (people == IN)
{
printf("%d th people live\n", i + 1);
break;
}
}
return 0;
}
华为面试题3
这是一道中级题. 高级题其实还不如这道题
1) 指出以下示意代码的错误之处
CRITICAL_SECTION g_CriticalSection=NULL;
char *g_string=NULL;
class MyClass
{
public:
int InitInstance();
int threadA();
};
int MyClass::Init()
{
InitCriticalSection(&g_CriticalSection);
CreateThread(threadA,...);
}
int MyClass::threadA()
{
EnterCriticalSection(g_CriticalSection)
if(g_string!=NULL)
{
delete g_string;
}
g_string = new char[1024];
...
delete g_string;
g_string = NULL;
ExitCriticalSection(g_CriticalSection)
return 0;
}
2) 如果MyClass改成如下定义,请写出实现上面功能的代码
class MyClass
{
static const CRITICAL_SECTION m_CriticalSection;
public:
MyClass() { EnterCriticalSection(m_CriticalSection); }
~MyClass(){ ExitCriticalSection(m_CriticalSection); }
};
(1)我随便说说看 我想问题应该在下面这里
int MyClass::Init()
{
InitCriticalSection(&g_CriticalSection);
CreateThread(threadA,...);
}
从代码来看,声明一个MyClass后,应该先调用Init()来初始化,问题就在初始化里面。千不该万不该,不该把临界区的初始化放在类的构造函数里面,这样一来,每初始化一个对象,就要把代表临界资源的变量给初始化一遍,如果这之前已经有对象进入了临界区,那么冲突是不可避免的了。要避免这种情况,最基本的办法就是把临界区的初始化放在一个全局的位置,毕竟临界区变量也是一个全局变量。
(2)如果要2)的要求实现的话
不知道这样行不行:
class MyClass
{
static const CRITICAL_SECTION m_CriticalSection;
public:
MyClass() { EnterCriticalSection(m_CriticalSection); }
~MyClass(){ ExitCriticalSection(m_CriticalSection); }
int threadA();
int Init();
};
int MyClass::Init()
{
CreateThread(threadA,...);
}
int MyClass::threadA()
{
if(g_string!=NULL)
{
delete g_string;
}
g_string = new char[1024];
...
delete g_string;
g_string = NULL;
return 0;
}
int InitCriticalSection(CRITICAL_SECTION* CriticalSection)
{
.............
};
开始时,先调用
InitCriticalSection(&MyClass.m_CriticalSection);
然后再使用MyClass对象