基本编程笔试题

1.定义一个“数据类型”datatype,能处理字符型,整形,浮点型三种数据类型,给出构造函数。

#include"stdafx.h"
#include
using namespace std;
class datatype
{
	enum
	{
		Inter, FLoat, Char
	}vartype;
	union 
	{
		char c;
		int i;
		float f;
	};
public:
	datatype(int ii)
	{
		vartype = Inter;
		i = ii;
	}
	datatype(char  cc)
	{
		vartype = Char;
		c = cc;
	}
	datatype(float ff)
	{
		vartype = FLoat;
		f = ff;
	}
	void print();
};
 void datatype::print()
{
	 switch (vartype)
	 {
	 case Inter:
		  cout << "整形:" << i << endl; break;
	 case Char:
		 cout << "字符型:" << c << endl; break;
	 case FLoat:
		 cout << "浮点型:" << f << endl; break;
	 default:
		 break;
	 }

}
 
int main()
{
	datatype A(12), B(16.44F), C('V');
	datatype D = A;
	A.print();
	B.print();
	C.print();
	D.print();
	return  0;
}

基本编程笔试题_第1张图片

2.用穷举法找出1-100之间的所有质数

#include"stdafx.h"
#include
using namespace std;
int main()
{
	bool bPrime=true;
	int iTotalNum=100;

	for (int i = 1; i <= iTotalNum; i++)
	{
		if (i == 1)
			bPrime = false;
		for (int j = 2; j<=sqrt(i); j++)
		{
			if (i%j == 0)
			{
				bPrime = false;	break;
			}
		}
		if (bPrime)
		{
			cout << i << " ";
		}
		bPrime = true;
	}
	return 0;

}
 

判断是否是质数

#include"stdafx.h"
#include
using namespace std;
bool Prime(int iNum)
{
	if (iNum == 1) return false;
	for (int i = 2; i < sqrt(iNum); i++)
	{
		if (iNum%i == 0)
			return false;
	}
	return true;
}
int main()
{
	bool bPrime = true;
	int iNum;
	while (cin >> iNum)
	{
		if (Prime(iNum))
			cout << iNum << "是质数" << endl;
		else
			cout << iNum << "不是质数" << endl;
	}
	return 0;

}

3.编写函数求两个函数的最小公倍数和最大公约数

 
#include"stdafx.h"
#include
using namespace std;
int GreatsetCommon(int A, int B);
int LeastCommon(int A, int B,int ileastcommon);
int main()
{
	int InterA,InterB;
	while (cin >> InterA&&cin >> InterB)
	{
		if (InterA==0||InterB==0)
		{
			cout << "最小公约数无,最大公约数为0!" << endl;
			continue;
		}
		int ileastcommon;
		ileastcommon= GreatsetCommon(InterA, InterB);
		LeastCommon(InterA, InterB,ileastcommon);
	}	 
    return 0;
}
int GreatsetCommon(int A,int B)
{
	int C;
   C = A%B;
	while (B!=0)
	{
		if (C == 0)
		{
			cout << "最大公约数是:" << B << endl; return B;
		}
		else
		{
			A = B; B = C; C = A%B;
		}
	}
	
}
int LeastCommon (int A, int B,int ileastcommon)
{
	int greastcommon;
	if (ileastcommon==0)
	{
		cout << "无最大公倍数!" << endl;
	}
	else
	{
		greastcommon = A*B / ileastcommon;
		cout << "最小公倍数为:" << greastcommon <

4.定义一个矩形类,有长,有宽两个属性,有成员函数计算矩形的面积

#include"stdafx.h"
#include
using namespace std;
class Rectangle
{
public:
	Rectangle(double length,double width);
	~Rectangle();
	double GetArea();
	double GetWidth();
	double GetLength();
private:
	double dLength;
	double dWidth;
};

double Rectangle::GetWidth() {return dWidth;}
double Rectangle::GetArea()  {return (dWidth*dLength); }
double Rectangle::GetLength(){return dLength; }
Rectangle::Rectangle(double length, double width)
{
	dLength = length; dWidth = width;
}

Rectangle::~Rectangle()
{
}
int main()
{
	cout << "请输入矩形的长和宽:" << endl;
	double length, width;
	while (cin>>length&&cin>>width)
	{
		Rectangle rTest= Rectangle(length, width);
		cout << "矩形的面积是:" << rTest.GetArea() << endl;
	}
	return 0;
}

基本编程笔试题_第2张图片

5.定义一个Shape基类,在此基础上派生出Rectangle和Circle,二者都在GetArea()函数,计算对象的面积,使用Rectangle类创建一个派生类Square

#include"stdafx.h"
#include
using namespace std;
#define PI 3.14
class Shape
{
public:
	Shape() {};
	~Shape() {};
	virtual float Area() = 0;
};
class circle :public Shape
{
public:
	circle(float radius) { fRadius = radius; };
	~circle() {};
	float Area() { return (float)(PI*fRadius*fRadius); };
private:
	float fRadius;
};
class rectangle:public Shape
{
public:
	rectangle(float length, float width) { fLength = length; fWidth = width; };
	~rectangle() {};
	float Area() { return fLength*fWidth; }
private:
	float fWidth;
	float fLength;
};
class square :public rectangle
{
public:
	square(float len) :rectangle(len, len) {};
	~square() {};
	float Area(float len) { return len*len; };
};

 
int main()
{
	Shape *s[3];
	s[0] = new circle(2);
	cout <<"圆的面积(2):"<< s[0]->Area() << endl;
	s[1] = new rectangle(2, 4);
	cout << "矩形面积(2,4):"<Area() << endl;
	s[2] = new square(3);
	cout << "正方形面积:" << s[2]->Area() << endl;
	for (int i = 0; i < 3; i++)
	{
		delete s[i];
	}
	return 0;
}

基本编程笔试题_第3张图片


6.为了得到一个数的"相反数",我们将这个数的数字顺序颠倒,然后再加上原先的数得到"相反数"。例如,为了得到1325的"相反数",首先我们将该数的数字顺序颠倒,我们得到5231,之后再加上原先的数,我们得到5231+1325=6556.如果颠倒之后的数字有前缀零,前缀零将会被忽略。例如n = 100, 颠倒之后是1. 

输入描述:
输入包括一个整数n,(1 ≤ n ≤ 10^5)

输出描述:
输出一个整数,表示n的相反数

输入例子1:
1325
输出例子1:
6556
#include
#include
using namespace std;
int main()
{
	 int iNum;
	 int iTemp;
	 int iOpsite=0; 
	 while(cin >> iNum)
	 { 
		 iTemp = iNum;
		 while (iTemp)
		 {
			 iOpsite = iOpsite * 10 + iTemp % 10;
			 iTemp /= 10;
		 }
		 cout << "相反数" << iOpsite << endl;
		 cout << iOpsite + iNum;
		 iNum = 0; iOpsite = 0;
	 }
	 
	return 0;
}















你可能感兴趣的:(C++算法实践)