腾讯面试题
2007-06-17 20:23
225 查看
题目:
class AAA
{
int a;
char b[5];
short c;
int d;
};
AAA* pA = 0x10000000;
问1: pA + 10= ?
问2:(char*) pA + 10 = ?
问3: (int*) pA + 10 = ?
回答:
1: 0x10000000 + 10 * 16 //双字节对齐
2: 0x10000000 + 10
3: 0x10000000 + 10 * 4
题目二:有一个集合,由0-1000的数字组成,要求写下列方法insert, erase, find, size, begin, end.要求性能最好。
memcpy函数的实现。
回答:
申请一个1001字节大小的数组,这就,要存放的数字与数组的下标就对应起来了,即:
1->a[1]
2->a[2]
另外,还要一个1000字节大小的数组b,b[1]里面存放值为a[1]的数据个数,b[2]里面存放值为a[12]的数据个数……
#include <iostream>
using namespace std;
const int ELEM_NUM = 1001;
int elemArr[ELEM_NUM] = {0};
void insert(const int nNum)
{
elemArr[nNum]++;
}
void erase(const int nNum)
{
if (elemArr[nNum] > 0)
{
elemArr[nNum]--;
}
}
bool find(const int nNum) //const
{
if (elemArr[nNum] > 0)
return true;
else
return false;
}
改为:
int find (int nNum)
{
return elemArr[nNum]; //即可以知道nNum是否存在,还知道存在多个少!
}
int begin() //const
{
int i = 0;
for (; elemArr[i] == 0 && i < ELEM_NUM; i++);
if (i == ELEM_NUM )
return -1;
else
return i;
}
int next(const int nNum) //const
{
int i = nNum + 1;
for (; elemArr[i] == 0 && i < ELEM_NUM; i++);
if (i == ELEM_NUM )
return -1;
else
return i;
}
int main()
{
int i = 0;
cout << "initiate the array please: /n";
while (i != -1)
{
cin >> i;
insert (i);
}
for (int i=begin(); i!=-1; i=next(i))
{
cout << i << ' ';
}
cout << "/ndelete some element: /n";
i = 0;
while (i != -1)
{
cin >> i;
erase (i);
}
cout << "find some element: /n";
i = 0;
while (i != -1)
{
cin >> i;
if (find (i))
cout << "elem found/n";
else
cout << "sorry, nothing found/n";
}
system ("pause");
}
class AAA
{
int a;
char b[5];
short c;
int d;
};
AAA* pA = 0x10000000;
问1: pA + 10= ?
问2:(char*) pA + 10 = ?
问3: (int*) pA + 10 = ?
回答:
1: 0x10000000 + 10 * 16 //双字节对齐
2: 0x10000000 + 10
3: 0x10000000 + 10 * 4
题目二:有一个集合,由0-1000的数字组成,要求写下列方法insert, erase, find, size, begin, end.要求性能最好。
memcpy函数的实现。
回答:
申请一个1001字节大小的数组,这就,要存放的数字与数组的下标就对应起来了,即:
1->a[1]
2->a[2]
另外,还要一个1000字节大小的数组b,b[1]里面存放值为a[1]的数据个数,b[2]里面存放值为a[12]的数据个数……
#include <iostream>
using namespace std;
const int ELEM_NUM = 1001;
int elemArr[ELEM_NUM] = {0};
void insert(const int nNum)
{
elemArr[nNum]++;
}
void erase(const int nNum)
{
if (elemArr[nNum] > 0)
{
elemArr[nNum]--;
}
}
bool find(const int nNum) //const
{
if (elemArr[nNum] > 0)
return true;
else
return false;
}
改为:
int find (int nNum)
{
return elemArr[nNum]; //即可以知道nNum是否存在,还知道存在多个少!
}
int begin() //const
{
int i = 0;
for (; elemArr[i] == 0 && i < ELEM_NUM; i++);
if (i == ELEM_NUM )
return -1;
else
return i;
}
int next(const int nNum) //const
{
int i = nNum + 1;
for (; elemArr[i] == 0 && i < ELEM_NUM; i++);
if (i == ELEM_NUM )
return -1;
else
return i;
}
int main()
{
int i = 0;
cout << "initiate the array please: /n";
while (i != -1)
{
cin >> i;
insert (i);
}
for (int i=begin(); i!=-1; i=next(i))
{
cout << i << ' ';
}
cout << "/ndelete some element: /n";
i = 0;
while (i != -1)
{
cin >> i;
erase (i);
}
cout << "find some element: /n";
i = 0;
while (i != -1)
{
cin >> i;
if (find (i))
cout << "elem found/n";
else
cout << "sorry, nothing found/n";
}
system ("pause");
}
相关文章推荐
- 2012腾讯实习面试题
- 腾讯面试题整理一
- 腾讯面试题集锦
- 经典面试题(一)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 腾讯面试题
- 腾讯面试题:查找单链表的中间节点
- 腾讯的面试题,据说很多人答不上来!
- 腾讯面试题(九度)——面积最大的全1子矩阵
- 2008腾讯的笔试面试题
- 腾讯面试题:tcp三次握手的过程,accept发生在三次握手哪个阶段?
- 一个腾讯的面试题
- 数据结构与算法学习笔记03(腾讯面试题)
- 一组数字1-n,随机取走3个,求被取走的数【腾讯前端面试题】
- 也做了一下腾讯前端面试题
- 尹成老师视频 中代码(关于递归(腾讯面试题的50台阶))
- 阿里、华为、腾讯Java技术面试题精选
- 腾讯面试题:50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法
- 腾讯php程序员面试题
- 腾讯PHP面试题
- 两道腾讯面试题