您的位置:首页 > 其它

2014新浪笔试题

2014-08-02 23:41 211 查看
1.内存对齐,sizeof

</pre><p><pre name="code" class="cpp">// sizeof考点
#include <iostream>
using namespace std;
struct A
{
char a;
short b;
int c;
};

class B
{
char a;
short b;
int c;
int fun()
{
cout<<a<<b<<endl;
}
};

class C
{
char a;
short b;
int c;
virtual int fun()
{
cout<<"virtual"<<endl;
}
};
void main(void)
{

short test;
cout<<sizeof(short)<<endl;
cout<<sizeof(A)<<endl;
cout<<sizeof(B)<<endl;
cout<<sizeof(C)<<endl;
}


//为什么一个空类的大小最小为1呢?是因为为了让类可以实例化,编译器自动给它添加了一个字节的大小(内存大小为0的对象不能存在与内存)

2.//写教务管理的类(STL实现)

#include <vector>
#include <algorithm>
#include<iostream>
using namespace std;
struct studentNode
{
int _age;
int _id;
string _name;
};

class StudentManager
{
public:
StudentManager();
//~StudentManager();
protected:
private:
int m_idIndex;
public:
vector<studentNode*> vecstudentmanager;
public:
bool addNewStudent(int age,const string& strName);//增加学生
bool deleteStudent(int id);//根据id删除学生
bool sortByAge();//使用STL中的sort函数对其进行排序
vector<int> findmaxAge();//找到最大的年纪
};

StudentManager::StudentManager()
{
m_idIndex=0;
}

bool StudentManager::addNewStudent(int age,const string& strName)
{
studentNode *SN=new studentNode[1];//必须用new,不然局部变量在函数return后会自动退栈
SN->_age=age;
SN->_id=++m_idIndex;
SN->_name=strName;
vecstudentmanager.push_back(SN);
return true;
}

bool StudentManager::deleteStudent(int id)//根据id删除学生
{
//首先进行遍历,找到所在位置
studentNode *stu;
stu=*(vecstudentmanager.begin());
vector<studentNode*>::iterator iter=vecstudentmanager.begin();
while (iter!=vecstudentmanager.end()&&stu->_id!=id)//遍历找到删除的位置,此时iter指向要删除的位置
{
++iter;
stu=*iter;
cout<<stu->_id<<stu->_age<<endl;
}
//由于vecstudentmanager保存的是指向studentNode的指针,所以应释放studentNode的内存,然后把vec中结点删除
delete[]stu;
vecstudentmanager.erase(iter);
return true;
}

bool CompareFun(studentNode *first,studentNode *second)
{
if (first->_age<second->_age)
{
return false;
}
else
{
return true;
}
}

bool StudentManager::sortByAge()
{
//用一个CompareFun定义比较规则,进行对结构体排序
sort(vecstudentmanager.begin(),vecstudentmanager.end(),CompareFun);
return true;
}

void main(void)
{
StudentManager SM;
SM.addNewStudent(5,"HENGYANG");
SM.addNewStudent(9,"leyuan");
SM.addNewStudent(7,"HENGYANG");
SM.addNewStudent(2,"leyuan");
// SM.deleteStudent(2);
SM.sortByAge();
}


3.

/*

//实现单词一级输出

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string>
struct Index
{
char *p;
struct Index *next;
}*pIndex;

void Create(Index *in,char *src)
{
Index *newNode=new Index;
newNode->next=in->next;
in->next=newNode;
newNode->p=new char[strlen(src)];
strcpy(newNode->p,src);
}

void main(void)
{
//新建一个26字母的数组
Index index[26];
for (int i=0;i<26;i++)//26个英文字母
{
index[i].next=NULL;
index[i].p=new char[2];//此处应声明为2,为之补0
for (int j=0;j<2;j++)
{
((index[i].p)[j])=0;
}

*(index[i].p)='a'+(char)i;
}
//通过fopen来读取txt文档
FILE *p;
p=fopen("..\\search.txt","r ");
char buf[1024]={0};
char *q=buf;
int count=0;
while (!feof(p))
{
fread(q++,1,1,p);
}
//对字符进行判断插入
q=buf;
char *t=q;
for (int i=0;i<20;i++)//20words
{
while (*t!='\0' && *t!='\n' && *t!=',')
{
t++;
}
//p指向的字符索引到指定的链表中
char *str=new char[t-q+1];
memset(str,0,t-q+1);
for (int j=0;j<t-q;j++)
{
str[j]=(*(q+j));
}
Create(&index[int((*q)-'a')],str);

t=t+1;
q=t;//继续下一趟循环
}
//查找功能
printf("请输入首字母");
char sh;
scanf("%c",&sh);

Index *ssh=&index[int((sh)-'a')];
while (ssh!=NULL)
{
cout<<ssh->p<<endl;
ssh=ssh->next;
}
}


//笔试完隔了好几天才回忆记下来的(现在发出来又是过了好几个月,题不怎么难)

4.考大数据排序

hash分治+quicksort

5.如何实现一个可靠的网络硬盘,给出具体的步骤(要求具备回滚能力,能回滚到任意状态)

hadoop可以不?这个题没答好

6.如何给微博用户做分组推荐

数据挖掘,聚类,具体巴拉巴拉~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: