您的位置:首页 > 理论基础

2015东南大学计算机系复试C++自己做的题解

2016-03-17 22:22 239 查看
明天就是复试的笔试环节,先做了去年的笔试题,除了没看懂题目的第三题,其他都做出来了。

这是题目:


第一题:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
double x,i=1.0;
double temp=1.0,sum=1.0,temp1=0.0;
cin>>x;
while(1)
{
temp1=temp;
temp*=x/i;
sum+=temp;
++i;
//cout<<temp<<" "<<temp1<<" "<<i<<endl;
if(fabs(temp-temp1)<=1e-7 && temp != temp1)
break;
}
cout<<setprecision(10)<<std::fixed<<sum<<endl;
}


第二题:

#include<iostream>
using namespace std;

void func(string s,int length)
{
if(length>=0)
{
if(s[length-1]<='9' && s[length-1]>='0')
{
cout<<s[length-1];
func(s,length-1);
}
else
{
func(s,length-1);
}
}
}

int main()
{
string s;
getline(cin,s);
func(s,s.length());
return 0;
}


第四题:

#include<iostream>
#include<string>
using namespace std;

void FindRepStr(char str[] , const char findStr[] , const char replaceStr[])
{
string s1(str),s2(findStr),s3(replaceStr);
int pos;
pos = s1.find(s2);
while(pos!=-1)
{
s1.replace(pos,s2.length(),s3);
pos = s1.find(s2);
}
cout<<s1<<endl;
}

int main()
{
//cin输入字符串太麻烦了,偷个懒
char * str = "helloworld";
char * s2 = "l";
char * s3 = "app";
FindRepStr(str , s2 , s3);
return 0;
}


第五题:

#include<iostream>
#include<string>
using namespace std;

class Teacher
{
public :
int number;
string name;
string sex;
int birthday;
int inday;

//构造函数
Teacher(int Number,string Name,string Sex,
int Birthday=19000000,int Inday)
{
number = Number;
name = Name;
sex = Sex;
birthday = Birthday;
inday = Inday;
cout<<"编号为:"<<number<<" 姓名为:"<<name<<
" 性别为:"<<sex<<" 生日为:"<<birthday
<<" 入职日期为:"<<inday<<endl;
}

//复制构造函数
Teacher(const Teacher &t)
{
number = t.number;
name = t.name;
sex = t.sex;
birthday = t.birthday;
inday = t.inday;

}

//2016是否退休
bool Retire_2016()
{
if(((2016 - birthday/10000) > 55 && sex == "male")
||(((2016 - birthday/10000) > 60) && sex == "female"))
{
cout<<"应退休的教师姓名为: "<<name<<" 编号为: "<<number<<endl;
return true;
}
else
return false;
}

//是否继续聘用女教师
bool Continue_retire_female()
{
if( (2016 - birthday/10000) > 60 && sex == "female" && (2016 - inday/10000) < 35)
{
cout<<"继续聘用的女教师姓名为:"<<name<<" 编号为: "<<number<<endl;
return true;
}
else
return false;
}
};

int main()
{
int number,birthday,inday;
string name,sex;
//自己选择测试数据的数量
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>number>>name>>sex>>birthday>>inday;
Teacher t(number,name,sex,birthday,inday);
if(t.Retire_2016())
{
//cout<<"应退休的教师姓名为: "<<t.name<<" 编号为: "<<t.number<<endl;
if(t.Continue_retire_female())
//cout<<"继续聘用的女教师姓名为:"<<t.name<<" 编号为: "<<t.number<<endl;

continue;
}

}
//测试复制构造函数
Teacher t(number,name,sex,birthday,inday);
Teacher t2(t);
cout<<t2.name<<" "<<t2.number<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: