您的位置:首页 > 其它

GameLoft智乐软件笔试题目誊抄练习——未完待续

2015-04-07 18:12 429 查看
#ifndef INTERVIEWGAMELOFT_H
#define INTERVIEWGAMELOFT_H

#include "Common.h"

// 笔试题有两份:一份基础 一份难,共计2小时。当天笔试完了估计第二天才通知机试吧。
// 机试是对原来的游戏进行添加和修改,类似于俄罗斯方块,处理一些判断,添加消除的功能,检查内存泄露,2个小时时间
/* 主要注意复习以下内容:
 * 位运算、函数的三种传值,
 * 32位色转为565的16位颜色
 * 和化积,叉积,Z缓存
 * 抢滩登陆游戏的UML建模
 *建议复习malloc,free,memset,strlen,strcmp,strcpy,const,指针等等
 */
// 2.Explain the need for "Virtual Destructor"
// 3.Explain "passing by value","passing by pointer" and passing by reference"
// 4.What is defference between "overloading"and "overriding"?
// 5. What is Memory Alignment?
// 6.What are inline functions?
// 7.What are the possible problems with implicit copy constructors?
// 8.How many memory leaks will this program hava? Please explain.
class Test_Q8
{
public:
	static Test_Q8 *get()
	{
		static Test_Q8 *eu  = 0;
		if( !eu )
			eu = new Test_Q8;
	}
};

void main_Test_Q8();
// 9.Will this program generate a memory leak? Why ?
void main_Test_Q9();
// 10.Explain what causes the compilation error.
class Vehicle_Q10
{
public:
	float weight;
	void dirven();
};
class ForWater_Q10: public Vehicle_Q10
{
public:
	void waves();
};
class ForLand_Q10:public Vehicle_Q10
{
public:
	void hoses();
};
class BothWaterAndLand_Q10:public ForWater_Q10, public ForLand_Q10
{
public:
	void setWeight(){ weight = 0.0f;}
};
void main_Test_Q10();
// 11.What is the problem with this code? Please explain.
class ClasaA{};
void main_Test_Q11();
// 12.What are the conditions which have to be accomplished in order for this code to compile?
// Why?
template<class T>
class ClasaTeste_Q12
{
private:
	T m_var;
public:
	void
};

// 第一道题:内存对齐是什么?
// 第二道题:指针,传地址,引用区别?
// 第三道题:隐式构造函数会导致什么问题?
// 第四道题:为什么需要虚拟析构函数?
// 第五道题:下面输出是什么?
class A_IVGL{
public:
	explicit A_IVGL(int a){ 
		cout<<a<<endl;
	}
	~A_IVGL(){ 
		cout<<"2"<<endl;
	}
	void Print() {
		cout<<"3"<<endl;
	}
};

class B_IVGL :public A_IVGL{
public:
	B_IVGL():A_IVGL(1){ 
		cout<<"4"<<endl;
	}
	virtual ~B_IVGL() { 
		cout<<"5"<<endl;
	}
	virtual void Print(){ 
		cout<<"6"<<endl;
	}
};

void InterviewGameLoftTest_5();

// 第六道题:给出许多类(人物,装备,交通工具,场景)让你画出UML图,注意类之间的继承关系。
// 第七道题:优化问题:
// 写一个函数判断是否是质数
bool IsPrime( int number );
bool fun( int num );

// 写一个函数,求两个参数之间的最大值。
int max_IVGL( int a, int b );

void max_test();

// 写一个函数:输出1到100的数字,如果数字是3的倍数则输出Nab,如果数字是5的倍数则输出Zif,
// 如果数字即是3的倍数也是5的倍数,则输出ZifBib,其他情况输出该数字。
void disp();

// 写一个函数,实现字符串的压缩存储,函数原型为void fun(char *str),假设输入字符串只含字母,压缩
// 方式为:如果字符串中出现相同连续的字符,则存储为字符的重复次数+字符本身,如果字符不连续出现,则
// 存储源字符,例如:strStr="ABBCCCDBB",则压缩后的字符串为"A2B3CD2B"。
#include <malloc.h>
void f(char *str);

void f_test();

// 将一个整数进行移位运算,左移时各个编译器均在最右侧移入0,右移时,。写一个函数
// 屏蔽掉编译器之间的差别,
int fun( int num ,int bit, int flag );

// 找出一个给定字符串里面出现频率最大的字符。
char GetMaxCountString( char *pStr );

// 写一个函数,short fun(int color)将一个32位的颜色值压缩为16位,压缩方式通常
// 为丢弃颜色值的低位,保留高位,32位颜色值的格式为XXXXXXXXRRRRRRRRGGGGGGGGBBBBBBBB,
// 假设高8位无作用。压缩后的16位颜色值应该为RRRRRBBBBBBGGGGG.
short fun( int color );/*
{
	return color & 0x0fff;
}*/

// 由静态函数想到的
class Point_IVGL{
public:
	void output(){}
	static void init(){}
};

class Point_IVGL_Init{
public:
	void output(){}
	static void init(){
	x = 1;
	y = 2;
	}
//private:
	static int x;
	static int y;
};

void InterviewGameLoftTest_static();

//////////////本代码文件的测试函数//////////////
void InterviewGameLoftTest();

#endif
#include "InterviewGameloft.h"

// 8.How many memory leaks will this program hava? Please explain.
void main_Test_Q8()
{
	for( int i = 0; i< 100; i++ )
		Test_Q8 *t = Test_Q8::get();
}
// 9.Will this program generate a memory leak? Why ?
void main_Test_Q9()
{
	char * p = new char[256];
	delete p;
}
// 10.Explain what causes the compilation error.
void main_Test_Q10()
{
	
}
// 11.What is the problem with this code? Please explain.
void main_Test_Q11()
{
	ClasaA *a = new ClasaA[256];
	delete a;
}

void InterviewGameLoftTest_5(){
	A_IVGL *a = (A_IVGL*) new B_IVGL;
	a->Print();
	//B *B_IVGL = (B_IVGL*) new C;
	//b->Print();
};
// 静态函数中不能操作非静态数据成员。
// 静态成员(函数)不属于任何一个具体的对象,那么在类的具体对象产生之前就已经有了内存区。
// 而非静态数据成员还没有分配内存空间,所以访问非静态数据成员是错误的。
// 还有一个原因是:静态成员函数没有隐含的this变量,所以,无法访问属于对象的数据成员,即非静态变量。
int Point_IVGL_Init::x = 1;
int Point_IVGL_Init::y = 2;
void InterviewGameLoftTest_static()
{
	Point_IVGL *pt = new Point_IVGL(); 
	//pt->init();//正确
	//pt.init();//正确
	//pt.output();//正确
	//Point_IVGL::output();//此处有错误
	///////////
	
	Point_IVGL_Init::init(); // 注意:此处编译没有错,在链接的时候发生了错误:
	// error LNK2001: 无法解析的外部符号 "private: static int Point_IVGL_Init::y" (?y@Point_IVGL_Init@@0HA)
	// 真正的原因是静态的成员变量需要进行初始化。参见Point_IVGL_Init_2类的定义。
	Point_IVGL_Init abc;
	cout<<abc.x;
	cout<<Point_IVGL_Init::y;
	Point_IVGL_Init *bc = new Point_IVGL_Init();
	cout<<bc->x;
	// 以上都可以访问,只是不能通过静态函数访问非静态变量而已。
}

// 写一个函数判断是否是质数
bool IsPrime( int number )
{
	number = abs(number);
	if( 0 == number || 1 == number )
		return true;
	int divisor;
	for( divisor = number/2; number %divisor != 0;  )
	{
		--divisor;
	}
	return 1 == divisor;
}

bool fun( int num )
{
	if( num == 0 || num == 1 )
		return false;
	if(  num == 2 || num == 3 )
		return true;
	int j,k;
	bool flag = true;
	k = (int)sqrt((float)num);
	for( j = 2; j <=k; j++ ){
		if( num %j == 0 ){
			flag = 0;
			break;
		}
	}
	if(flag)
		return true;
	else
		return false;
}

//// 写一个函数,求两个参数之间的最大值。
int max_IVGL( int a, int b ){
	int buf[2] = {a,b};
	unsigned int z;
	z = a -b;	
	z >>= 31;	
	return buf[z];
}

void max_test()
{
	int a_fun =29;
	if( fun(a_fun ) )
		cout<<a_fun<<" is prime"<<endl; //测试质数
	for( int i = 0; i < a_fun; i ++ ){
		int max_IVGL_1 = rand()%a_fun;
		int max_IVGL_2 = rand()%a_fun;
		cout<<max_IVGL_1<<" , "<<max_IVGL_2<<" 之间的最大值是:"<<max_IVGL(max_IVGL_1,max_IVGL_2)<<endl;
	}
}

// 写一个函数:输出1到100的数字,
void disp()
{
	for( int i = 1; i <= 100; i++ )
	{
		if( (i%3 == 0) && ( i%5 == 0) ){
			cout<<i<<"ZifBib"<<endl;
			continue;
		}
		if( i%3 == 0 ){
			cout<<i<<"Nab"<<endl;
			continue;
		}
		if( i%5 == 0 ){
			cout<<i<<"Zif"<<endl;
			continue;
		}
		cout<<i<<" "<<endl;
	}
}

// 写一个函数,实现字符串的压缩存储
void f( char *str)
{
	char *des = (char *)malloc(100*sizeof(char));
	char *s = des;
	int cnt = 0;
	while( *str )
	{
		cnt++;
		str++;
		if( *str != *(str-1) ){
			if( cnt!= 1){
				*des = cnt + 0x30;
				des++;
			}
			*des = *(str-1);
			des++;
			cnt=0;
		}
	}
	des++;
	*des = '\0';
	cout<<*s;
}

void f_test()
{
	char *s = "ABBCCCDBB";
	f(s);
}

// 将一个整数进行移位运算,左移时各个编译器均在最右侧移入0,右移时,。写一个函数
// 屏蔽掉编译器之间的差别,
int fun( int num ,int bit, int flag )
{
	int s,i;
	for( i = 0; i < bit; )
	{
		s = num & 0x8000000;
		num >>= 1;
		if( flag && s )
			num |= 0x80000000;
		else
			num &= ~0x80000000;
	}
	return num;
}
}

////////代码测试//////////
void InterviewGameLoftTest()
{
	//InterviewGameLoftTest_5();
	//InterviewGameLoftTest_static();
	//max_test();
	f_test();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: