【超级完整-更正版】北京理工大学计算机专业复试机试历年真题答案2003年-2018年

该题是北京理工大学2003-2018年的计算机专业考研复试机试真题,以下题目是我简写的,代码注重逻辑,有些可能没有规范输出。如果有任何疑问,可私信我或者直接去查年份对应的题目参考综合学习。

目录

    • 1-2003两个空间坐标求球的体积
    • 2-2003计算某一天是一年的第几天
    • 3-2003学生根据学号进行文件的读取
    • 1-2004多项式的类去递归求值
    • 2-2004计算正弦函数重载减号
    • 3-2004一元二次方程求值
    • 1-2005复数类的封装和重载复数相加
    • 1-2006打印直角三角形
    • 2-2006数字的逆序输出
    • 1-2007递归求小球下落弹起到静止的长度
    • 2-2007运算符重载计算两个坐标的距离
    • 3-2007学生类的增删改
    • 4-2007计算三个坐标是否构成直角三角形
    • 1-2008一个日期加上天数变成新的日期
    • 2-2008计算两个日期相差多少天
    • 3-2008除数是0或者数组越界捕捉异常
    • 1-2009字符串处理+另存文件
    • 2-2009统计分组元素
    • 3-2009统计单词词频
    • 1-2010中缀转二叉树输出
    • 1-2011-统计首字母相同的单词
    • 3-2011-单词排序
    • 4-2011-表达式中截取数字主要针对两位数以上的数字
    • 1-2012-学生对象的插入删除
    • 2-2012-数字排序
    • 3-2012-后序中序确定树输出前序
    • 1-2013-最小公约数公倍数
    • 2-2013-字符串排序
    • 3-2013-运算符优先级
    • 4-2013-二叉树中序转前序+运算符优先级
    • 1-2014-输出三个最近使用文件有重复就不打乱文件顺序
    • 2-2014-输出最近使用三个文件
    • 3-2014-求广义表的深度
    • 2-2015-输出限制字符长度的全排列
    • 3-2015-火车输出序列堆栈的使用
    • 4-2015-单词排序不区分大小写
    • 1-2016-字符串中摘取数字
    • 2-1016-指定精度计算PI值
    • 3-2016-数字转字符串
    • 4-2016-链表存奇偶数
    • 5-2016-动态开辟数组数目
    • 6-2016递归求字符串的长度
    • 7-2016-单词排序
    • 1-2017-统计学生没及格输出
    • 2-2017-限制字符串的全排列
    • 3-2017-字符串中的元素排序
    • 4-2017-身份证校验码
    • 5-2017-全排列
    • 6-2017-求字符串的全部子串
    • 7-2017-判断n是否在n的平方最右边
    • 8-2017-二分查找
    • 9-2017-打印菱形
    • 10-2017-打印等腰三角形
    • 1-2018-求字符集交集
    • 2-2018-求一个偶数是哪两个素数之和
    • 3-2018-奇偶数拆分为两个链表
    • 4-2018-回文的判断

1-2003两个空间坐标求球的体积

#include

#include

#include

#include

#define Max 100

double const PI=3.141592;

using namespace std;

class  ThreeDimensionalSpace{

public:

double x;

double y;

double z;

public :

ThreeDimensionalSpace(double x1,double y1,double z1)

{

x = x1;

y = y1;

z = z1;

}

double operator - (ThreeDimensionalSpace b);

};

double ThreeDimensionalSpace::operator - (ThreeDimensionalSpace b)

{

return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y)+(y-b.y)*(y-b.y));

}

int main()

{

double x1,y1,z1;

double x2,y2,z2;

while(1)

{

cout<<"输入球心坐标"<>x1>>y1>>z1;

cout<<"输入球表面的一个坐标"<>x2>>y2>>z2;

ThreeDimensionalSpace a(x1,y1,z1);

ThreeDimensionalSpace b(x2,y2,z2);

double r = a-b;

double V = (4/3)*PI*r*r*r;

cout<

3-2003学生根据学号进行文件的读取

#include

#include

#include

#include

#include

using namespace std;

int main()

{

while(1)

{

cout<<"输入功能: 1、写入文件 2、根据学号查询信息"<>id;

switch(id)

{

case 1:

{

ofstream outfile;

outfile.open("D:\\FileStore.txt");

outfile<<"01 李男 21"<>number;

cout<

1-2004多项式的类去递归求值

#include

#include

#include

#include

#define Max 100

using namespace std;

class  MutipleClass{

public:

double x;

int n;//vector大小

vectorvec;//[Max];

MutipleClass(double y,vectorv,int size)

{

x =y;

vec = v;

n =size;

}

double GetResult(vector,int n,double x)

{

if(n==0)

{

return vec[n];

}else

{

return GetResult(vec,n-1,x)+vec[n]*pow(x,n);

}

}

};

int main()

{

int n;

cout<<"输入系数个数"<>n)

{

double x;

cout<<"输入x的值"<>x;

cout<<"输入系数"<vec;

for(int i=0;i>t;

vec.push_back(t);

}

MutipleClass m(x,vec,vec.size());

cout<>x>>y)

{

Angle a(x);

a.Sin();

Angle b(y);

b.Sin();

Angle t = a-b;

t.Sin();

}

}

3-2004一元二次方程求值

#include

#include

#include

 double const PI = 3.1415;

using namespace std;

int main()

{

double a,b,c;

cout<<"输入a b c"<>a>>b>>c)

{

if(a==0)

{

cout<<"a不能为零,重新输入"<>x1>>y1>>x2>>y2)

{

VirtualNumber a(x1,y1);

VirtualNumber b(x2,y2);

VirtualNumber c = a+b;

c.show();

double x = a;

cout<

1-2006打印直角三角形

#include

#include

#include

using namespace std;

class Cpoint{

double x,y;

public:

Cpoint(double xx,double yy){

x =xx;

y =yy;

}

double operator-(Cpoint c);

};

double Cpoint::operator-(Cpoint c){

return sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));

}

class CTriangle{

public:

double x1,y1;

double x2,y2;

double x3,y3;

public:

CTriangle (int a,int b,int c,int d,int e,int f)

{

x1=a;

y1=b;

x2=c;

y2=d;

x3=e;

y3=f;

}

CTriangle operator+(CTriangle c);

void BuildTriangle()

{

Cpoint A(x1,y1);//在纵坐标上

Cpoint B(x2,y2);//原点

Cpoint C(x3,y3);//横坐标

int len1 = A-B;

int len2 =B-C;

int len3 =A-C;

cout<<"三角形是"<

2-2006数字的逆序输出

#include

#include

#include

#include

using namespace std;

//从字符串中取出数字

void GetString(string str,vector&vec)

{

int pos = 0;

for(int i=0;ivec;

vectorvex;

//while(cin>>str)

//{

GetString(str,vec);

//控制十个数量

for(int i=0;i

1-2007递归求小球下落弹起到静止的长度

#include

#include 

using namespace std;

double GetPath(double h,int n)

{

if(n==1)

{

//小心第一次是小球从高处落到底就是一次,路径就是H

return h;

}else

{



return h+h/2+GetPath(h/2,n-1);

}

}

int main()

{

int m;

    //cin>>m;

    double h;

int n;

    while(cin>>m)

    {

        for(int i=0;i>h>>n;

         //printf("%.2lf\n",GetPath(h,n));

         //控制C++输出精度

         cout << setprecision(2) << setiosflags(ios::fixed);

         cout<

2-2007运算符重载计算两个坐标的距离

#include

#include

#include

using namespace std;

class Cpoint{

double x,y;

public:

Cpoint(double xx,double yy){

x =xx;

y =yy;

}

double operator-(Cpoint c);

};

double Cpoint::operator-(Cpoint c){

return sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));

}

int main()

{

cout<<"输入第一个坐标"<>x1>>y1;

Cpoint a(x1,y1);

cout<<"输入第二个坐标"<>x2>>y2;

Cpoint b(x2,y2);

cout<<"两个坐标之间的距离是"<

3-2007学生类的增删改

#include

#include

#include

#include

using namespace std;

class Student{

public:

char name[10];

int num;

public:

Student(){

}

Student(char n[10],int m)

{

strcpy(name,n);

num =m;

}

public:

void show()

{

cout<<"姓名:"<y.num;

}

bool compareAsc(Student x,Student y)

{

return x.numvec;

int id;

char name[10];

int number;

while(1){

cout<<"输入需要的功能,1:添加学生 2:查询某个学生 3:降序浏览学生 4:升序浏览学生"<>id;

switch(id)

{

case 1:

{

cout<<"输入添加的学生数目"<>n;

cout<<"输入学生名字 学号"<>name>>number;

//strcpy(names,name);

Student stu(name,number);

vec.push_back(stu);

}

break;

}

case 2:

{

cout<<"输入学生学号"<>number;

int flag=0;

for(int i=0;i

4-2007计算三个坐标是否构成直角三角形

#include

#include

#include

using namespace std;

class Cpoint{

double x,y;

public:

Cpoint(double xx,double yy){

x =xx;

y =yy;

}

double operator-(Cpoint c);

};

double Cpoint::operator-(Cpoint c){

return sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));

}

class CTriangle{

double x1,y1;

double x2,y2;

double x3,y3;

public:

CTriangle (int a,int b,int c,int d,int e,int f)

{

x1=a;

y1=b;

x2=c;

y2=d;

x3=e;

y3=f;

}

void Print()

{

Cpoint A(x1,y1);

Cpoint B(x2,y2);

Cpoint C(x3,y3);

int len1 = A-B;

int len2 =B-C;

int len3 =A-C;

if(len1>len3)//需要len3大

{

swap(len1,len3);

}

if(len2>len3)//需要len3大

{

swap(len2,len3);

}

if(len3*len3 == len2*len2+len1*len1)

{

cout<>m){



int x1,y1,x2,y2,x3,y3;

while(m--)

{

cin>>x1>>y1>>x2>>y2>>x3>>y3;

CTriangle Test(x1,y1,x2,y2,x3,y3);

Test.Print();

}

}

}

1-2008一个日期加上天数变成新的日期

#include

#include

#include

#include

using namespace std;

int daytab[2][13]={

{0,31,28,31,30,31,30,31,31,30,31,30,31},//平年

{0,31,29,31,30,31,30,31,31,30,31,30,31}//闰年

};

bool IsLeapYear(int year)

{

return(year%4 == 0&& year%100 !=0)||(year%400==0);

 }

int MuchDay(int year,int month,int day)

{

int number =0;

int row = IsLeapYear(year);

for(int j=0;jNumberOfYear(year))

{

number -=NumberOfYear(year);

year++;

}

month =0;

int row = IsLeapYear(year);

while(number>daytab[row][month])

{

number -=daytab[row][month];

month++;

}

day =number;

cout<

2-2008计算两个日期相差多少天

#include

#include

#include

#include

using namespace std;

int daytab[2][13]={

{0,31,28,31,30,31,30,31,31,30,31,30,31},//平年

{0,31,29,31,30,31,30,31,31,30,31,30,31}//闰年

};

bool IsLeapYear(int year)

{

return(year%4 == 0&& year%100 !=0)||(year%400==0);

 }

int MuchDay(int year,int month,int day)

{

int number =0;

int row = IsLeapYear(year);

for(int j=0;jmonth2)

{

apart = MuchDay(year1,month1,day1)-MuchDay(year2,month2,day2);

}

else{

apart = MuchDay(year2,month2,day2)-MuchDay(year1,month1,day1);

}

}else//不在同一个年里

{

if(year1>year2)//如果第一个年大

{

if(IsLeapYear(year2))//把第二个年的剩余天数加起来

{

apart += 366 -MuchDay(year2,month2,day2);

}else{

apart += 365 -MuchDay(year2,month2,day2);

}

for(int i=year2+1;i

3-2008除数是0或者数组越界捕捉异常

#include

#include

using namespace std;

class A{

};

class B{

};

int main()

{

cout<<"输出被除数和除数"<>a>>b;

vectorarr;

try{

if(b==0)

{

throw A();

}else

{

cout<<"结果是"<>length;

cout<<"输入数组元素"<>n)

{

if(n==0)break;

arr.push_back(n);

}

if(arr.size()>length)

{

throw B();

}

for(int i=0;i

1-2009字符串处理+另存文件

#include

#include

#include

#include

#include

using namespace std;

int main()

{

string str;

int i=1;

string ss="";

while(cin>>str)

{

stringstream scout;//数字转字符

scout<

2-2009统计分组元素

#include

#include

#include

#define Max 100

using namespace std;

struct Child{

int data;

int count;

};

struct Answer{

int group;

Child child[Max];

};

void getString(vector&vec,string str)

{

//vectorvec;

int pos;

for(int i=0;ivecp;

vectorvecd;

getString(vecp,parents);

getString(vecd,child);

//父母数组中的元素  

vectortemp1 =vecp;

sort(temp1.begin(),temp1.end());

temp1.erase(unique(temp1.begin(),temp1.end()),temp1.end());

int n1 = temp1.size();

// 孩子数组中的元素

vectortemp2 =vecd;

sort(temp2.begin(),temp2.end());

temp2.erase(unique(temp2.begin(),temp2.end()),temp2.end());

int n2 = temp2.size();



Answer *answer = new Answer[vecp.size()];

//初始化 ,用一个结构体,包含孩子数组叫groupid,父母数组的元素和每个元素的出现次数count

for(int i=0;i

3-2009统计单词词频

#include

#include

#include

#define Max 100

using namespace std;

struct Word{

string words;

int number;

}arr[Max];

bool Compare(Word a,Word b)

{

return a.number>b.number;

}

int main()

{

string str="I am a teacher and I think you are a student,so i think you lie.";

int pos=0;

vectorvec;

vectorvex;

for(int i=0;i

1-2010中缀转二叉树输出

#include

#include

#include

#include

#include

using namespace std;

/*

中缀建立二叉树的思路:

1.预处理字符串:把字符串中的每个部分都切割出来放进vector,因为存在有点数字是两位数以上,不能用字符串的下标去定位操作数。

2.先中缀转后缀

(1)符号进站,操作数直接输出,比较新来的符号和栈顶,栈顶大于或等于新来符号,循环输出栈顶直至栈顶不大于等于新来符号

3.后缀建立二叉树

(1)New一个结点,把结点data等于操作数然后进栈

(2)遇到符号,在栈中pop出两个元素进行,先后把结点的右孩子data赋值第一个元素,左孩子data赋值给第二个元素

*/

/*

遇到的几个新颖技术点:

1.字符转字符串:头文件 string s(1,字符),输出s就是字符串了

*/

typedef struct TreeNode{

string tag;

int data;

TreeNode *rchild,*lchild;

}Tree;

int Priority(char a,char b)//a是top,b是准备进栈元素

{

int p;

switch(a)

{

case '+':

case '-':

if(b=='*'||b=='/')

{

p= -1;break;

}else if(b=='+'||b=='-')

{

p= 0;break;

}



case '*':

case '/':

if(b=='*'||b=='/'){

p=0;break;

}else if(b=='+'||b=='-')

{

p=1;break;

}

case '(':

p = -1;break;

}

return p;

}

bool judge(char a)//判断是数字

{

int temp = a- '0';

for(int i=0;i<10;i++)

{

if(temp==i)

{

return true;

}

}

return false;

}

void GetString(vector&vec,string str){

  int pos=0;

  for(int i=0;i'0'&&str[0]<'10')

  {

  return true;

 }

 else

 {

  return false;

 }

 }

void PrintTree(TreeNode *T){

if(T==NULL)

{

return;

}

PrintTree(T->lchild);

PrintTree(T->rchild);

cout<data<<" ";

}

int Deal(int a,int b,string x)

{

if(x=="*")

{

return a*b;

} else if(x=="/")

{

return b/a;

}else if(x=="-")

{

return b-a;

}else if(x=="+")

{

return a+b;

}

}

void BuildTree(vector vex)//通过后缀表达式建立二叉树

{

stackss;//放后缀转树的栈

TreeNode *T = NULL;

for(int j=0;jdata  =n;

T->lchild =NULL;

T->rchild =NULL;

ss.push(T);

}else//操作符

{//pop出两个元素并处理

int a,b;

T = new TreeNode;

if(!ss.empty())

{

a= ss.top()->data;

T->rchild =ss.top();

ss.pop();

}

if(!ss.empty())

{

b= ss.top()->data;

T->lchild =ss.top();

ss.pop();

}

T->data = Deal(a,b,vex[j]);//处理加减乘除

ss.push(T);

}

}

PrintTree(T);

}

int main()

{

stackst;//转后缀的栈

//ss;//放后缀转树的栈

vector vec;//存放处理后的中缀表达式

vector vex;//存放后缀表达式

//string str="2*(70-2)+50*20+80";

string str="1+((2+3)*4)-5";//12 3 + 4 * + 5 –

GetString(vec,str);//把每个字符放进vec,因为有两位数以上的数字,这个方法就是解决这个问题,把每个部分都放进vector

//以下算法为中缀转后缀

for(int i=0;i

1-2011-统计首字母相同的单词

#include

#include

#include



using namespace std;



GetWord(vector&vec,string str)

{

int pos;

for(int i=0;ivec;

GetWord(vec,str);

//去重复元素

vec.erase(unique(vec.begin(),vec.end()),vec.end());

for(int i=0;i

2-2011-领导关系

#include 

#include 

#include 

using namespace std;

/*

3. 给一个字符串(aaaa(bbbb(cccc,dddd),eeee(ffff)))该字符串

表明的是各人的层次关系

比如aaaa是bbbb和eeee的领导,bbbb是cccc和dddd的领导

现输入一个名称,比如ffff,要求输出其领导关系

输出:aaaa>eeee>ffff

*/

 /*

//(aaaa(bbbb(cccc,dddd),eeee(ffff)))

   //top  3    2    1         0     1

   如ffff

   在top==1 && 字符为'('时,处理字符串,在结果字符串中:加入eeee> ,变成eeee>ffff

   在top==2 && 字符为'('时,处理字符串,在结果字符串中:加入aaaa>,变成aaaa>eeee>ffff

   top==3时,满足,这里不写出来,因为后面我们会去掉

   同理:

    如eeee

//(aaaa(bbbb(cccc,dddd),eeee(ffff)))

   //top  2    1    0         1    

   在top==1 && 字符为'('时,处理字符串,在结果字符串中:加入aaaa> ,变成aaaa>ffff

   同理top==2不写出来

    */

int main()

{

string str1, str2, str="";

vector vc;

str1="(aaaa(bbbb(cccc,dddd),eeee(ffff)))";

str2="ffff";

int tag=0;

int pos = str1.find(str2);

str.append(str2);

int count=0;//领导的长度

int index=0;

int number=0;//和tag搭配使用,当tag = 1+number时候就是遇到领导的时候



for(int i=pos;i>=0;i--){//思路,处理字符串,匹配括号,截取字符串 联想到括号匹配,这里对栈的处理进行了变形

if(str1[i]==')'){//给括号标号

tag--;

}else if(str1[i]=='('){

tag++;

}

if(index!=0){

if(str1[i]==',' or str1[i]=='('){

//index是包括领导的长度,所以要减去领导长度,截取出领导

string s = str1.substr(index-count, count);

cout<");

index = i;//Index记录的是领导后面的一个(或者,

}

}

//cout<

    return 0;

}

3-2011-单词排序

#include

#include

#include



using namespace std;



GetWord(vector&vec,string str)

{

int pos;

for(int i=0;ivec;

GetWord(vec,str);

//去重复元素

//vec.erase(unique(vec.begin(),vec.end()),vec.end());

sort(vec.begin(),vec.end());

for(int i=0;i

4-2011-表达式中截取数字主要针对两位数以上的数字

#include

#include

#include

#include

void GetString(vector&vec,string str){

  //vector vec;

  int pos=0;

  string test="";

  //2*(70-20)+50*20

  for(int i=0;i vec;

GetString("2*(70-2)+50*20");

}

1-2012-学生对象的插入删除

#include

#include

#include

#define Max 100

using namespace std;

class Student{

public:

 int id;

 string name;

 char sex;

 int age;

Student(){

this->id = NULL;

this->name = "";

this->sex = NULL;

this->age = NULL;

}

Student(int id,string name,char sex,int age)

{

this->id = id;

this->name = name;

this->sex = sex;

this->age = age;

}

int GetId() const {

        return this->id;

    }

    void SetId(int id) {

        this->id = id;

    }

    string GetName(){

        return this->name;

    }

    void SetName(int name) {

        this->name = name;

    }

    int GetAge() const {

        return this->age;

    }

    void SetAge(int age) {

        this->age = age;

    }

    char GetSex() {

        return this->sex;

    }

    void SetSex(int sex) {

        this->sex = sex;

    }

};

bool Compare(Student x,Student y)

{



//return x.GetId() > x.GetId();

return x.id > y.id;

}

void GetInfo(string str,string &id,string &name,char &sex,int &age)

{

int k=1;

int pos=0;

for(int i=0;ivec;

while(getline(cin,info))

{



string oid;

string sname;

char ssex;

int sage;

if(info[0]=='I')//插入

{

GetInfo(info,oid,sname,ssex,sage);

string tempid = oid.substr(1);

int id = stoi(tempid);

Student stu(id,sname,ssex,sage);

vec.push_back(stu);

k++;

}

else//删除

{

string tempid = info.substr(1);

int id = stoi(tempid);

for(int i=0;i::iterator it;

for(it = vec.begin(); it != vec.end(); ++it)

{

cout<<(*it).id<<" ";

cout<<(*it).name<<" ";

cout<<(*it).age<<" ";

cout<<(*it).sex<

2-2012-数字排序

#include

#include

#include

using namespace std;

void GetNumber(string str)

{

vectorvec;

int size = str.size();

int pos =0;

for(int i=0;i

3-2012-后序中序确定树输出前序

#include

#include

#include

using namespace std;

struct TreeNode{

char data;

TreeNode *left,*right;

}Tree;

TreeNode* Create(string str1,string str2)//后序+中序

{

if(str1.size()==0)

{

return NULL;

}

char end = str1[str1.size()-1];//根节点

//cout<data = end;

int rithtlen =str2.size()-pos-1;//中序右边长度

int leftlen = str2.size()-pos;//中序左边长度

p->right = Create(str1.substr(str1.size()-rithtlen-1,rithtlen),str2.substr(pos+1)

);

p->left = Create(str1.substr(0,pos),str2.substr(0,pos));

return p;

}

void Preorder(TreeNode* T)

{

if(T==NULL)

{

return;



}

printf("%c",T->data);

Preorder(T->left);

Preorder(T->right);

return ;

}

int main()

{

string str1="CHBEDA";

string str2="CBHADE";

//while(cin>>n)

//{

int pos=0;

TreeNode* root =Create(str1,str2);

Preorder(root);

printf("\n");



//}

return 0;

}

1-2013-最小公约数公倍数

#include

using namespace std;

int main()

{

int m=15;

int n=20;

int z=m;

if(m

2-2013-字符串排序

#include

#include

#include

using namespace std;

int main()

{

string str;

while(getline(cin,str))

{

vectorvec;

int size = str.size();

int j = 0;

for(int i=0;i::iterator it;

// for(it=vec.begin();it!=vec.end();it++)

// {

// cout<<*it<<" ";

// }

// cout<::iterator tt;

for(tt=vec.begin();tt!=vec.end();tt++)

{

cout<<*tt<<" ";

}

cout<

3-2013-运算符优先级

#include

#include

using namespace std;

int  Priority(char s1,char s2)

{//判断两个运算符s1,s2的优先顺序,

//若s1优先则返回>,若s2优先则返回<,若s1,s2相同则返回=

char f;

int p;

switch(s2)

{

case '+':

case '-':

if(s1=='*'||s1=='/')p=-1;

else if(s1=='+'||s1=='-')p=0;break;

case '*':

case '/':

if(s1=='+'||s1=='-')p= 1;

else if(s1=='*'||s1=='*')p= 0;break;

}

return p;

}

void  Reverse(string all)

{

string temp;

for(int i=all.size()-1;i>-1;i--)

{

temp +=all[i];

}

cout<

4-2013-二叉树中序转前序+运算符优先级

#include

#include

#include

using namespace std;

int  Priority(char s1,char s2)

{

char f;

int p;

switch(s2)

{

case '+':

case '-':

if(s1=='*'||s1=='/')p=-1;//+ * 返回-1,栈顶优先级大

else if(s1=='+'||s1=='-')p=0;break;

case '*':

case '/':

if(s1=='+'||s1=='-')p= 1;

else if(s1=='*'||s1=='*')p= 0;break;

case '(':

case ')':

p=0;break;

}

return p;

}

bool Alphabet(char c)//判断是不是字母

{

for(int i=0;i<26;i++)

{

char te ='a'+i;

if(c==te)

{

return true;

}

}

return false;

}

string Reverse(string all)//逆序字符串

{

string temp;

for(int i=all.size()-1;i>-1;i--)

{

temp +=all[i];

}

return temp;

}

int main()

{

/*

算法:

从右开始,是字符就添加进字符串,是右括号进进栈,如果是运算符,

和栈顶比较,栈顶优先级大或者相等就进栈。栈顶优先级小就出栈。出栈的组成字符串,最后逆转字符串输出。

*/

stacktag;

string str="a+b*(c-d)-e/f";

cout<0;i--)

{

if(Alphabet(str[i]))

{

all +=str[i];

}

else if(str[i]==')')

{

tag.push(str[i]);

}

else if(str[i]=='(')

{

char c = tag.top();

while(c!=')'&&!tag.empty())

{



tag.pop();

all +=c;

c = tag.top();

}

tag.pop();

}else if(tag.empty()||Priority(str[i],tag.top())==-1 || Priority(str[i],tag.top())==0)

{

//栈顶优先级大就进栈

//栈为空进站

//优先级相同进站

tag.push(str[i]);

}else if(Priority(str[i],tag.top())==1)

{

//栈顶优先级小就出栈

char c = tag.top();

tag.pop();

all +=c;

tag.push(str[i]);

}

}

while(!tag.empty())//把栈中剩余的元素输出

{

char c = tag.top();

tag.pop();

all +=c;

}

cout<

1-2014-输出三个最近使用文件有重复就不打乱文件顺序

#include

#include

#include

using namespace std;

bool FindHistory(stackst,int n)

{

int size = st.size();

for(int i=0;ist;

int n;

cout<<"输入:";

while(cin>>n)

{

cout<<"输出:";

if(FindHistory(st,n))

{

stacksq =st;

int size = sq.size();

for(int i=0;isq =st;

int size = sq.size();

for(int i=0;i

2-2014-输出最近使用三个文件

#include

#include

#include

using namespace std;

int main()

{

stackst;

int n;

cout<<"输入:";

while(cin>>n)

{

int num=0;

st.push(n);

cout<<"输出:";

stacksq =st;

int size = sq.size();

if(size<3)

{

for(int i=0;i

3-2014-求广义表的深度

#include

#include

#include

using namespace std;

int main()

{

//stackst;

stackst;

int n;

string str="((c,((d.(e)),f,h),g)";

int max=0;

for(int i=0;imax)

{

max = st.size();

}

}else if(str[i]==')')

{

int size = st.size();

for(int i=0;i

#include

int main()

{

    int i,j;

    int test=11;//只能输入奇数

    int n=(test+1)/2;

    //单独输出第一行

    for(int k=0;k<2*n-1;k++)

    {

     printf("*");

}

printf("\n");



//上半个长方形

    for(i = 1; i < n; i++)//从第二行开始,打印一半的行数

    {

     int temp =n-1 - i;//输出直角三角形

        for(j = 0; j <= temp; j++)

        {

            printf("@");

        }

        for(j = 0; j < 2 * i - 1; j++)//输出正等腰三角形

        {

            printf(" ");

        }

        for(j = 0; j <= temp; j++)//输出后半个三角形。别想太复杂 ,和前面个三角形代码一样

        {

            printf("&");

        }

        printf("\n");    

    }

    //输出下半个长方形

    for(i = 0; i < n-2; i++)//最后一行,和第一行一样,都是直接输出完整一行,就减去2,头一行,尾巴一行

    {

     int temp = i + 1;

        for(j = 0; j <= temp; j++)//输出直角梯形 ,第一行输出两个

        {

            printf("O");

        }

        

        for(j = 0; j < 2 * n-2*i-5; j++)//输出空格的倒三角,根据图形去推理的的公式

        {

            printf(" ");

        }

        for(j = 0; j <=temp; j++)//和前一个直角梯形一样的代码

        {

        

         printf("*");

        }

        printf("\n");    

    }

    //最后一行完整输出

    for(int k=0;k<2*n-1;k++)

    {

     printf("*");

}

}

2-2015-输出限制字符长度的全排列

#include 

#define M 256

using namespace std;

char buf[M] = {0};

void fun(int n, int size,string str)//形参n表示当前字符为第n个字符,size表示字符的个数

{

if (n > size)

{

buf[n-1] = '\0';

puts(buf);//输出字符串并换行

return;

}

for (int i = 0; i < str.size(); i++)

{

buf[n-1] = str[i];//不停的覆盖,就实现了更新每个位置的字符

fun(n+1, size,str);

}

return;

}

int main()

{

int n = 2;

string str="abc";

fun(1, n,str);//第一个参数始终是1,表示从第一个字符开始处理

return 0;

}

3-2015-火车输出序列堆栈的使用

#include

#include

#include



using namespace std;

int main()

{

stackC;

queueB;

queueD;

stackA;

int n=6;

for(int i=n;i>0;i--)

{

A.push(i);

}

while(!A.empty()||!B.empty()||!C.empty())

{

int trainA = A.top();

A.pop();



if(trainA%2==0)

{

if(C.size()<3||C.size()==3)

{

C.push(trainA);

cout<C ";

}

else{

int trainC = C.top();

C.pop();

//A.push(trainC);

cout<A ";

}

}else

{

if(B.size()<3||B.size()==3)

{

B.push(trainA);

cout<B ";

}

else

{

int trainB = B.front();

B.pop();

//D.push(trainB);

cout<D ";

}



}

while(!B.empty()&& A.empty() )

{

int trainB = B.front();

B.pop();

//D.push(trainB);

cout<D ";

}

while(!C.empty()&&A.empty())

{

int trainC = C.top();

C.pop();

//D.push(trainB);

cout<A ";

}

}

 }

4-2015-单词排序不区分大小写

#include

#include

#include

using namespace std;

struct Node{

string a,b;

};

int compare(Node x,Node y)

{

return x.b='A'&&ss[i].b[j]<='Z')//把所有字母转换成小写字母

{

ss[i].b[j] +='a'-'A';

}

}

}

sort(ss,ss+7,compare);

for(int i=0;i<7;i++)

{

cout<

1-2016-字符串中摘取数字

#include

#include

#include

using namespace std;



bool judgeA(char a)

{

for(int i=0;i<26;i++)

{

if((a=='a'+i)||(a=='A'+i))

{

return true;

}

}

return false;

}

int judgeN(string s)

{

for(int i=0;i0;i--)

{

if(s[i]=='0' && flag ==1)

{

char tt =s[i];

//cout<&vec,string s)

{

//vectorvec;

int pos=0;

//0+123

for(int j=1;j&vec,string s)//处理字符串

{

vectorvex;

NumberCon(vex,s);

for(int i=0;ivec;

//string str="2.3ABC0-2.3+004.5000ABC+000123.300";

//string str ="002.3+3000+34A+00.3000";

//string str="2.3ABC0-2.3";

//string str="2.3ABC0-2.3+004500";//2.3 0 -2.3 +45

string str="+004.500";

string temp="";

string last="";

int flag =0;//没有出现过+ -

for(int i=0;i

2-1016-指定精度计算PI值

#include

#include

#include 

using namespace std;

int  main()

{

float pi=0;

float accurate =  0.01;

//while(cin>>accurate)

//{

float n=4;

float t =3;

float temp=0;

int k=1;

pi = n;

while((n/t)>accurate)

{

if(k%2==0)

{

pi += n/t;

}else

{

pi -= n/t;

}

t +=2;

k++;

//cout<

3-2016-数字转字符串

#include

#include

#include

using namespace std;

void judge(double a)

{

ostringstream mos;//构造字符串流

mos<

4-2016-链表存奇偶数

#include

#include

#include 

using namespace std;

typedef struct LNode{

int data;

struct LNode * next;

}LNode,*LinkList;

string trim(string &s)

{

for(int i=0;i&vec)

{

int j=0;

for(int i=0;ivec;

vec.push_back(number);

}

}

string te=s.substr(j);

int num = stoi(te);

vec.push_back(num);//最后一个子串加进去

}

void PrintL(LinkList L)

{

LinkList t = L->next;

while(t!=NULL)

{

cout<data<<" ";

t= t->next;

}

cout<next==NULL)

{

temp =(LNode*)malloc(sizeof(LNode));

temp->data = n;

L->next = temp;

temp->next = NULL;

//cout<<"头插入成功"<next!=NULL)&&(p->next->data>n))

{

p = p->next;

}

temp =(LNode*)malloc(sizeof(LNode));

temp->data = n;

temp->next = p->next;

p->next = temp;

//cout<next=NULL;

LinkList Olist =(LinkList)malloc(sizeof(LinkList));

Olist->next=NULL;

vectorvec;

GetNumber(str,vec);

for(int i=0;ivec;

// GetNumber(str,vec);

// for(int j=0;j

5-2016-动态开辟数组数目

#include

#include

#include

using namespace std;

const int MAX=100;



struct student{

string name;

string id;

int score[MAX];

int nopass;//没有及格数

};

student arr[MAX];

void init(int n,int m)

{

for(int i=0;iy.nopass;

}

int main()

{

int n=5;

int m=3;

arr[0].name="飞机";arr[0].score[0]=70;arr[0].score[1]=80;arr[0].score[2]=91;

arr[1].name="轮船";arr[1].score[0]=20;arr[1].score[1]=20;arr[1].score[2]=20;

arr[2].name="汽车";arr[2].score[0]=60;arr[2].score[1]=80;arr[2].score[2]=90;

arr[3].name="单车";arr[3].score[0]=50;arr[3].score[1]=50;arr[3].score[2]=60;

arr[4].name="卡卡";arr[4].score[0]=30;arr[4].score[1]=60;arr[4].score[2]=60;

init(n,m);

sort(arr,arr+n,Compare);

for(int i=0;i

6-2016递归求字符串的长度

#include

#include

#include 

using namespace std;

int forlength(string s,int i)

{

if(s[i]=='\0')

{

return 0;

}

else

{

return 1+forlength(s,i+1);

}

}

int  main()

{

string str;

while(cin>>str)

{

cout<

7-2016-单词排序

#include

#include

#include

using namespace std;

GetWord(vector&vec,string str)

{

int pos;

for(int i=0;ivec;

GetWord(vec,str);

//去重复元素

//vec.erase(unique(vec.begin(),vec.end()),vec.end());

sort(vec.begin(),vec.end());

for(int i=0;i

1-2017-统计学生没及格输出

#include

#include

#include

using namespace std;

const int MAX=100;

struct student{

string name;

int c1;

int c2;

int c3;

};

student arr[MAX];

student didnt[MAX];

bool Compare(student x,student y)

{

int  a=x.c1+x.c2+x.c3;

int  b=y.c1+y.c2+y.c3;

return a>b;

}

int main()

{

int n=5;

//while(cin>>n)

//{

// for(int i=0;i>arr[i].name>>arr[i].c1>>arr[i].c2>>arr[i].c3;

//

// }

arr[0].name="飞机";arr[0].c1=70;arr[0].c2=80;arr[0].c3=91;

arr[1].name="轮船";arr[1].c1=20;arr[1].c2=20;arr[1].c3=20;

arr[2].name="汽车";arr[2].c1=60;arr[2].c2=70;arr[2].c3=80;

arr[3].name="单车";arr[3].c1=50;arr[3].c2=50;arr[3].c3=50;

arr[4].name="卡卡";arr[4].c1=30;arr[4].c2=60;arr[4].c3=60;

int k=0;

for(int i=0;i

2-2017-限制字符串的全排列

#include 

#define M 256

using namespace std;

char buf[M] = {0};

void fun(int n, int size,string str)//形参n表示当前字符为第n个字符,size表示字符的个数

{

if (n > size)

{



buf[n-1] = '\0';

puts(buf);//输出字符串并换行

return;

}

for (int i = 0; i < str.size(); i++)

{

buf[n-1] = str[i];//不停的覆盖,就实现了更新每个位置的字符

fun(n+1, size,str);

}

return;

}

int main()

{

int n = 2;

string str="abc";

fun(1, n,str);//第一个参数始终是1,表示从第一个字符开始处理

return 0;

}

3-2017-字符串中的元素排序

#include

#include

#include

#include

using namespace std;

 int main()

 {

  vectorvec;

  string str="ac cf bgn oo qq pp";

  //cout<

4-2017-身份证校验码

#include

using namespace std;

int main()

{

char ID[]={'1','0','X','9','8','7','6','5','4','3','2'};

//cout<>Ai;

int S=0;

for(int i=0;i

5-2017-全排列

#include

#include

#include

#include



//算法思想,参考博客

//https://blog.csdn.net/weixin_43145361/article/details/89061051

using namespace std;

vectorall;

void traverse(vectora,vectorb)//递归实现博客中所描述的树

{

if(a.size()>0)//a是第一个原来的字符串,b是排列的字符串

{

for(int i =0;i::iterator it;

for(int i=0;ivec;

vectorresult;

string str="abc";

for(int j=0;j::iterator it;

for(it=all.begin();it!=all.end();++it)//输出全排列字符串

{

cout<<*it<

6-2017-求字符串的全部子串

#include

#include

#include

using namespace std;

int main()

{

//https://blog.csdn.net/weixin_43871369/article/details/90523139

string str;

while(cin>>str)

{

mapnumber;

for(int i =0;i<=str.size();i++)

{

for(int j=0;j

7-2017-判断n是否在n的平方最右边

#include

#include

#include

using namespace std;

int change(int n)

{

int num=0;

while(n)

{

n = n/10;

num++;

}

return num;

 }

 int main()

 {

  int n=1;

  //int m1= 123456;

//cout<>n)

//{

int square = n*n;

int lenn=change(n);//计算数字n的长度

int lens=change(n*n);//计算数字n的平方的长度

char* a=new char[lenn];

char* b=new char[lens];

    itoa(n,a,10);//把n转为字符串

    itoa(square,b,10);////把n的平方转为字符串

  for(int i=0;i

8-2017-二分查找

#include

using namespace std;

void BFind(int a[],int n,int m)

{

int low=0;

int high=n-1;

int count=0;

int mid;

while(low<=high)

{

count++;

mid=(low+high)/2;

if(a[mid]==m)

{

cout<<"第"<m)

{

high = mid-1;

}else

{

low = mid+1;

}

}

cout<<"查找失败 比较次数是"<>n)

  {

  BFind(a,10,n);

 }

// BFind(a,10,24);

// BFind(a,10,120);

// BFind(a,10,6);

 }

9-2017-打印菱形

#include

using namespace std;

int main()

{

int n=21;

int test =(n+1)/2;

for(int i=0;i

10-2017-打印等腰三角形

#include

using namespace std;

 int main()

 {

  int n=10;

  for(int i=1;i<=n;i++)

  {

  for(int j=1;j<=n-i;j++)

  {

  cout<<" ";

 }

  for(int j=1;j<=2*(i-1)+1;j++)

  {

  cout<<"*";

 }

 cout<

1-2018-求字符集交集

#include

#include

#include

#include

using namespace std;

 const int MAX =1000;

char len[MAX];

 int main()

 {

  string str1="bcdaopk",str2="abefpko";

//cin>>str1>>str2;

int k=0;

for(int i =0;i

2-2018-求一个偶数是哪两个素数之和

#include

#include

#include

using namespace std;

bool judgePrime(int n)//判断素数

{

int k=sqrt(n);

int i;

for(i=2;i<=k;i++)

{

if(n%i==0)

{

break;

}

}

if(i>k)

{

return true;

}

else

{

return false;

}

}

int main()

{

/*

算法:

1.t=n;n=n-1,去判断是不是素数

是:则再去判断 t-n是不是素数,是就满足条件

不是:n=n-2;继续去循环去判断n



*/

int n=10;//n是偶数

int t=n;

n--;//n是偶数,减一是奇数

int k =t/2;//只判断一半,比如n=10,则只需要计算到1~5的数字就够了,因为该题问的是一个偶数由那两个奇数组成。

while(n>=k)

{

if(judgePrime(n))

{

int m=t-n;//组成n的其中一个元素,比如t=10,这里的m=1,n=9;

if(judgePrime(m))//判断m是不是素数

{

cout<

3-2018-奇偶数拆分为两个链表

#include

#include

#include



using namespace std;

/*

 *输入字符串,分别将偶数奇数拆分在两条链表上存储

 */

typedef struct Lnode{

    int data;//数据域

    struct Lnode *next;    //指针域

}Lnode,*Linklist;

void PrintL(Linklist L)

{

Linklist t = L->next;

while(t!=NULL)

{

cout<data<<" ";

t= t->next;

}

cout<next==NULL)

    {

     temp = (Lnode*)malloc(sizeof(Lnode));

temp->data= m;

temp->next = NULL;

L->next = temp;

return;

}



    while((p->next!=NULL)&&(p->next->datanext!=NULL不能少

     p =p->next;

  }

temp = (Lnode*)malloc(sizeof(Linklist));

temp->data= m;

temp->next = p->next;

p->next = temp;

return;

}

int main()

{

Linklist h1 =  (Linklist)malloc(sizeof(Linklist));

h1->next =NULL;//偶数链表

Linklist h2 =  (Linklist)malloc(sizeof(Linklist));

h2->next=NULL;//存奇数链表

string str="246137";

//while(cin>>str)

for(int i=0;i>str;

//while(cin>>str)

//{

stackst;

int i=0;

for(;i

using namespace std;

 int main()

 {

  int n=5;

  //while(cin>>n)

  //{

  for(int i=1;i<=n;i++)

  {

  for(int j=1;j<=n-i;j++)

  {

  cout<<" ";

}

for(int j=1;j<=2*i+n-2;j++)

/*

/比如n=4

i     j

第一行:3空格4*

第二行:2空格6* 

第三行:1空格8*

第四行:0空格10* 

刚好 j=2*i+n-2

*/

  {

  cout<<"*";

}

cout<

你可能感兴趣的:(考研)