您的位置:首页 > 职场人生

腾讯面试题

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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: