c++机考试题
1、 反复读入正整数,并判断它是否为素数 (质数),若是,则输出\,否则,输出\,
直到按下ctrl+D时,程序终止执行。
#include
int main() { int x;
bool y,z=true; while(1) {
cout<<\ cin>>x; y=prime(x);
if(y==true) cout<<\ else cout<<\ }
return 0; }
bool prime(int x) {
bool i=true; int j=sqrt(x);
for(int k=2;k<=j;k++) {
if(x%k==0){i=false;break;} }
return i; } 2、 编程实现:从键盘输入任意多个整数(以ctrl+d结束输入),输出第三大的整数。
3、 提示用户从键盘输入两正整数:m,n,调用函数GCD(),LCM()分别求这两个数的最大公约
数、最小公倍数
#include
int GCD(int x,int y);
int LCM(int x,int y); int main() {
int x,y,a,b;
cout<<\请输入两个整数:\ cin>>x>>y; cout< cout<<\他们的最大公约数是:\ cout<<\他们的最小公倍数是:\ return 0; } int GCD(int x,int y) { int m; if(x m=y,y=x,x=m; if(x%y==0) return y; return GCD(x,x%y); //辗转相除法 } int LCM(int x,int y) { int i=x/GCD(x,y)*y; //最小公倍数=两数之积/最大公约数,先除后乘可以在一定程度上防止大数 return i; } 4、 从键盘输入一行英文,统计输出下列字符的个数:(1)小写字母(2)大写字母(3)数 字(4)空格(5)其他 5、 从键盘输入n(n<10),然后输出: 1 1 2 2 3 3 3 5 5 5 5 5 8 8 8 8 8 8 8 8 13 13 13 13 13 13 13 13 13 13 13 13 13 #include long int fibo(int n) { if(n==1||n==2) return 1; else return fibo(n-1)+fibo(n-2); } int main() { int m,n,k=1; cout<<\ cin>>n; for(int i=1;i<=n;i++) { m=fibo(k); for(int j=1;j<=m;j++) cout< if(k>n) break; } return 0; } 6、 编写函数DelRepeat(),将有序数组a中的重复元素删除,如a[]={1,5,5,5,9,9,11, 13,13,13,17,19} 中的重复数据删除后,变成{1,5,9,11,13,17,21}(注数组中的元素递增有序) 7、 随机生成50个1~15的正整数,将之存放在数组a中,设计函数count(),找出数组a中 出现频率最高的元素,及其出现的次数。 #include void count(int a[],int n) { int b[15]={0}; for(int i=1;i<=n;i++) b[a[i]]++; int max=0; for(int j=1;j<15;j++) { if(b[i]>b[max]) max=i; } cout<<\最多的数\出现了\次\} int main() { srand(time(NULL)); int a[50]; for(int j=1;j<=50;j++) a[j-1]=rand()I+1; count(a,50); return 0; } 8、 编写函数factor(int n ),实现输出整数n的所有因子。 #include int main() { int x; cout<<\ cin>>x; factor(x); cout< void factor(int x) { for(int i=1;i<=x;i++) { if(x%i==0) cout< 9、 键盘输入起止年份y1,y2,并输出y1~y2间的所有闰年,要求每行输出8个年份 10、 已知CDate类的声明及部分成员函数的定义如下: class CDate { public: CDate(int y=1, int m=1, int d=1){SetDate(y,m,d);} void SetDate(int day, int month, int year); CDate &operator++(); CDate operator++(int ); friend ostream& operator<<(ostream &out,CDate c); 编程实现CData的++运算符重载,在定义一个CDate对象 a,测试++a; a++; friend CDate operator+(CDate c,int day); private: 11、 CDate类的定义同于10题,编程实现流插入符<<的重载(非成员函数),实现对CDate int Day; 对象的输出,在主函数中定义CDate类的对象c,则cout< int DaysPerMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 的随机数排序,并输出。 Year=y; 15、 从键盘输入n,生成n个1~100的随机整数,编写函数bubbleSort(int a[], int n),将生 if(IsLeapYear()) DaysPerMonth[2] =29; 成的随机数排序,并输出。 Month=(m>12||m<1)?1:m; 16、 编写程序从键盘输入一个整数(可以为任意整数,只要不超过int的表示范围即可), Day=(d>DaysPerMonth[Month])?1:d; 编写函数bool Judge(int n)判断其是否为回文数,如12321、123321均是回文数,是} 则返回true,否则返回false。 …… 17、 读取任意一个英文文本文件,统计该文件中英文单词的个数。 18、 从键盘读入一行字符串(若干单词、空格组成),编写函数DelBlank(),将该字符串中 多余的空格删除(即两单词之间有且仅有一个空格)。 19、 已知基类shape、派生类sphere(球体)及cylinder(圆柱体)的部分定义如下: class shape { public: virtual double SurfaceArea()=0; //表面积 virtual double Volume()=0; //体积 }; class sphere:public shape { 请完成球体类、圆柱体类的其他成员函数的实现并对之进行测试,要求: public: (1) 从键盘输入球体的半径、圆柱体的半径及高 sphere(int r=0):radius(r){} (2) 多态调用,分别求出球体、圆柱体的表面积、体积 void SetRadius(double r){ radius=(r>0)?r:0;} 20、 实现Point类、Rectangle类,其中矩形类(Rectangle)由左上角的点p1,右下角的点 double SurfaceArea(); p2确定。要求实现求矩形的长、宽、面积。(提示:Rectangle类有两个数据成员p1,p2, double Volume(); 其类型是Point类) private: 21、 动态数组类的声明及部分成员函数的实现如下: double radius; }; class cylinder:public shape { public: cylinder(int r,int h):radius(r),height(h){} void SetData(double r,double h){radius=r;height=r;} class MyArray { public: MyArray(int s=10); MyArray(const MyArray &a); MyArray &operator=(const MyArray &a); ~MyArray(){ delete []arr;} int &operator[](int i); //重载下标运算符[] int GetSize(){return size;} void reverse(); //将arr中的元素逆置 void Display(); private: int *arr; int size; }; MyArray::MyArray(int n) { assert(n>0); arr=new int[n]; size=n; } MyArray::MyArray(const MyArray &a) { size=a.size; arr=new int[size]; for(int i=0;i 22、 编写程序,将斐波拉契数列前40项写到文件test.dat文件中,然后从文件test.dat文 件中读出斐波拉契数列的各奇数项,并在屏幕上输出,每行5个(注意对齐) 23、 实现类Complex,重载运算符-(取反),>>(流插入运算符),<<(流提取运算符),并 测试 class Complex { public: Complex(double r = 0.0, double i = 0.0); Complex operator- ( ); friend ostream & operator << (ostream &out, const Complex &c); friend istream & operator >> (istream&in ,Complex &c); private: double real; double imag; }; 24、 实现类Complex,要求重载+(复数的加法)、-(复数的减法)运算符、=(赋值运算 符重载),复数的输出可用普通成员函数实现。必须有测试。 class Complex { public: Complex(double r = 0.0, double i = 0.0); Complex operator + (const Complex &c2) const; Complex operator - (const Complex &c2) const; Complex &operator=(const Complex &c2); void display( ) const; private: double real; double imag; };