C++ 数据结构、算法笔试题(1)

来源:互联网 发布:韩国电影《招待》 知乎 编辑:程序博客网 时间:2024/06/13 23:41

<span style="background-color: rgb(255, 255, 255); font-family: Simsun;">1、</span><span style="background-color: rgb(255, 255, 255); font-family: Simsun;">定义一个”数据类型” datatype</span><span style="background-color: rgb(255, 255, 255); font-family: Simsun;"><span style="background-color:inherit"><strong>类</strong></span></span><span style="background-color: rgb(255, 255, 255); font-family: Simsun;">,能处理包含字符型、整型、浮点型三种类型的数据,给出其</span><span style="font-family: SimSun; font-size: 12px; background-color: rgb(255, 255, 255); "><strong><span style="font-family: Simsun; font-size: 14px;"><span style="background-color:inherit">构造函数</span></span><span style="font-family: Simsun; font-size: 14px;">。</span></strong></span>

<pre name="code" class="cpp">#include <iostream>using namespace std;class datatype{private:    enum{characer,integer,floating}vartype;    union    {        char c;        int i;        float f;    };public:    datatype(char cc){vartype=characer;c=cc;}    datatype(int ii){vartype=integer;i=ii;}    datatype(float ff){vartype=floating;f=ff;}    void print();};void datatype::print(){    switch(vartype)    {        case characer:            cout<<"字符型:"<<c<<endl;break;        case integer:            cout<<"整形:"<<i<<endl;break;        case floating:            cout<<"浮点型:"<<f<<endl;break;    }}int main(){    datatype a('a'),b(1),c(2.2f);    a.print();    b.print();    c.print();    return 0;}

2、用穷举法找出1~100间的质数,显示出来

#include <iostream>using namespace std;bool If_Prime(int i);int main(){    for(int i=2;i<=100;i++)    {        if(If_Prime(i))            cout<<i<<" ";    }    return 0;}bool If_Prime(int i){    for(int j=2;j<=i/2;j++)    {        if(i%j==0)            return false;    }    return true;}


3、在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

#include <iostream>using namespace std;int main(){    int i;    cout<<"Enter a integer(1~100): ";    cin>>i;    int j;    cout<<"Guess the number: ";    cin>>j;    while(i!=j)    {        if(i<j)            cout<<"larger! Guess again~"<<endl;        else if(i>j)            cout<<"less! Guess again~"<<endl;        cin>>j;    }    cout<<"Right!"<<endl;    return 0;}

4、编写函数求两个整数的最大公约数和最小公倍数。

#include <iostream>using namespace std;int GCD(int a,int b);   //最大公约数int LCM(int a,int b);   //最下公倍数int main(){    int a,b;    cout<<"输入两个整数:";    cin>>a>>b;    int gcd=GCD(a,b);    int lcm=LCM(a,b);    cout<<"最大公约数:"<<gcd<<endl;    cout<<"最小公倍数:"<<lcm<<endl;    return 0;}int GCD(int a,int b){    if(a&&b)    {        int Min=a<b? a:b;        for(int i=Min;i>0;i--)        {            if(a%i==0&&b%i==0)                return i;        }    }    else        return -1;}int LCM(int a,int b){    if(a&&b)    {        int Max=a>b? a:b;        for(int i=Max;i<a*b;i++)        {            if(i%a==0&&i%b==0)                return i;        }    }    else return -1;}

5、编写递归函数GetPower(int x, int y)计算x的y次幂, 在主程序中实现输入输出。
#include <iostream>using namespace std;int GetPower(int x,int y);int main(){    int x,y;    cin>>x>>y;    int result=GetPower(x,y);    cout<<result;    return 0;}int GetPower(int x,int y){    if(y==0)        return 1;    else if(y==1)        return x;    else return x*GetPower(x,y-1);}

6、定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积
#include <iostream>using namespace std;class Rectangle{private:    float length;    float width;public:    Rectangle(int l,int w):length(l),width(w){}    ~Rectangle(){}    double Square();};double Rectangle::Square(){    return length*width;}int main(){    float len,wid;    cout<<"输入矩形的长和宽:";    cin>>len>>wid;    Rectangle r(len,wid);    double square=r.Square();    cout<<square;    return 0;}
</pre><pre name="code" class="cpp">
</pre><span style="color: rgb(255, 0, 0); font-family: Simsun;font-size:14px; line-height: 24px;">7、编写一个函数,统计一个英文句子中字母的个数,在主程序中实现输入、输出</span><pre name="code" class="cpp">#include <stdio.h>#define mSize 100using namespace std;int Num(char* a);int main(){    char a[mSize];    cout<<"输入一个英文句子:";    gets(a);        //使用gets()从键盘输入,头文件<stdio.h>    cout<<"ok"<<endl;    int num=Num(a);    cout<<"字母的个数:"<<num;    return 0;}int Num(char* a){    int count = 0;    int i=0;    while(a[i]!='\0')    {        if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))            count++;        i++;    }    return count;}


8、编写一个矩阵转置的函数,矩阵的维数在程序中由用户输入。

9.编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的位置,如果在s中没有与t匹配的子串,就返回-1。

#include <iostream>#include <cstring>#define mSize 100using namespace std;int Order(char* s,char* t);int main(){    char s[mSize];    char t[mSize];    cin>>s;    cin.clear();    cin.sync();    cin>>t;    int ord=Order(s,t);    cout<<ord<<endl;    return 0;}int Order(char* s,char* t){    int len_s=strlen(s);    int len_t=strlen(t);    if(len_s<len_t||len_s<0||len_t<0)        return -1;    int ps=0,pt=0,p=0;    while(p<len_s&&len_s-p>=len_t)    {        while(s[ps]==t[pt])        {            ps++;            pt++;            if(t[pt]=='\0')                return p+1;        }        p++;        pt=0;        ps=p;    }    return -1;}


10.编写函数reverse(char *s)的倒序递归程序,使字符串s倒序。
#include <iostream>#include <cstring>#define mSize 100void Reverse(char* s);using namespace std;int main(){    char s[mSize];    cin>>s;    Reverse(s);    cout<<s;    return 0;}void Reverse(char* s){    int len=strlen(s);    if(len<=0)        return;    int i=0,j=len-1;    char temp;    while(i<j)    {        temp=s[i];        s[i]=s[j];        s[j]=temp;        i++;        j--;    }}


11.一个Shape基类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。
#include <iostream>using namespace std;class Shape{public:    virtual double GetArea()=0;};//这里不用声明构造函数和析构函数!!!!!!!!class Rectangle:public Shape{private:    double length;    double width;public:    Rectangle(double a,double b):length(a),width(b){}    ~Rectangle(){}    double GetArea()    {        return length*width;    }};class Circle:public Shape{private:    double r;public:    Circle(double i):r(i){}    ~Circle(){}    double GetArea(){return 3.14*r*r;}};int main(){    Rectangle rec(4,5);    Circle cir(4);    cout<<rec.GetArea()<<endl;    cout<<cir.GetArea()<<endl;    return 0;}


0 0