2014

1,写出计算Ack(m,n)的递归算法

复制代码
#include<iostream>
using namespace std;
int A(int m,int n){
	if(m==0){
		return n+1;
	}
	else if(m>0&&n==0){
		return A(m-1,1);
	}
	else{
		return A(m-1,A(m,n-1));
	}
}

	
int main(){
	int m,n;
	cout<<"please input two number"<<endl;
	cin>>m>>n;
	cout<<A(m,n)<<endl;
	return 0;
}

2,写一个 IntToStr(int a)函数将一个整形数转换为字符串

复制代码
//法一
#include<iostream>
#include<string>
#include<sstream> 
using namespace std;
string intToString(int num)
{
	stringstream s;
	s<<num;
	string str;
	s>>str;
	return str;
}
	
int main(){
	cout<<intToString(3)<<endl;
	return 0;
}

//法二
#include<iostream>
#include<string>
#include<sstream> 
using namespace std;
string intToString(int num)
{
	string r =" "; 
	while(num){
		int n =num%10;
		r =char('0'+n)+r;//将数字转换成字符拼接到r前面 
		num =num/10; 
	}
	return r;
}
	
int main(){
	cout<<intToString(15)<<endl;
	return 0;
}

3,//写一个 swap(int a[], int m, int n ),使得数组的前 m 项和后 n 项交换位置

复制代码
//写一个 swap(int a[], int m, int n )
//使得数组的前 m 项和后 n 项交换位置
#include<iostream>
using namespace std;
void reverse(int a[],int m,int n){
	while(m<n){
		int temp = a[m];
		a[m] = a[n];
		a[n] = temp; 
		m++;
		n--;
	}
}
void swap(int a[],int m,int n,int k){
	reverse(a,0,m-1);//反转前m个数 
	reverse(a,m,k-n-1);//反转中间的数 
	reverse(a,k-n,k-1);//反转后n个数 
	reverse(a,0,k-1);//反转整个数组 
}
	
int main(){
	int a[]={1,2,3,5,6,7};
	int k= 6;
	swap(a,2,2,k);
	for(int i=0;i<k;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}

4,

复制代码
//(1)写一个日期 Date 类,成员有年月日,成员函数有无参数构造函数、设置年月日的函数 setDate,还有一个打印函数 display
//(2)第二个类是员工类 Employee,成员有工号、姓名、身份证号、出生日期、受聘日期、聘用年限、月薪
//成员函数要有构造函数、改变出生日期函数、改变聘用年限函数、改变月薪函数、续聘函数(要求当续聘后的年龄大于 60 时给提示不能续聘)
//还有展示函数 display,需要有工号、姓名、身份证号、出生日期、聘用到期时间、聘用年限、年薪,注意第二个类会有 Date 类或其指针作为成员
#include<iostream>
#include<string>
using namespace std;
class Date{
	public:
		Date(int y=0,int m= 0,int d= 0):year(y),month(m),day(d){}
		Date(Date &d){
			this->year = d.year;
			this->month =d.month;
			this->day =d.day ;
		}
		void setDate(int y,int m,int d){
			year = y;
			month = m;
			day = d;
		}
		int getyear() const{
			return year;
		}
		int getmonth()const{
		return month;
		}
		int getday() const{
		return day;
		}
		void display()const{
			cout<<year<<"-"<<month<<"-"<<day; 
		}
	private:
		int year;
		int month;
		int day;
};
class Employee{
	public:
		Employee(long id,string name,long number,Date birthday,Date workday,int workyear,double salary){
			this->id = id;
			this->name = name;
			this->number = number;
			this->number = number;
			this->birthday = birthday;
			this->workday = workday;
			this->workyear = workyear;
			this->salary = salary;
		}
		void changeBirthday(Date newdate){
			birthday = newdate;
		}
		void changeworkyear(int newyear){
			workyear = newyear;
		}
		void changesalary(double newsalary){
			salary = newsalary;
		}
		void renewContract(int time) {  // 续聘
        if (birthday.getyear()+time > 60){
        	cout<<"续聘不成功"<<endl; 
		}
		else
		{
			workyear+=time; 
		}
	}
	//还有展示函数 display,需要有工号、姓名、身份证号、出生日期、聘用到期时间、聘用年限、年薪
		void display()const{
			 cout << "工号:" << id << " 姓名:" << name << " 身份证号:" << number << " 出生日期:";
             birthday.display();
			cout<<"聘用到期时间"<<workday.getyear() +workyear<<"年"<<workday.getmonth()<<"月"<<workday.getday() <<"日";
			cout<<"聘用年限"<<workyear<<"年薪"<<salary<<endl; 
			 
		}
		private:
		long id;
		string name;
		long number;
		Date birthday;
		Date workday;
		int workyear;
		double salary;
	};
	
int main(){
	Date birth(1990, 10, 25);  // 创建出生日期对象
    Date hire(2020, 5, 15);  // 创建聘用日期对象

    Employee emp(1001, "John Doe", 1234567890, birth, hire, 3, 5000.0);  // 创建员工对象

    emp.display();  //
	emp.renewContract(20);
	return 0;
}
相关推荐
Coovally AI模型快速验证25 分钟前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
mit6.82427 分钟前
[openvela] Hello World :从零开始的完整实践与问题复盘
c++·嵌入式硬件
pusue_the_sun28 分钟前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
RaymondZhao341 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
zhangfeng11331 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
啊阿狸不会拉杆2 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路2 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程3 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
你知道网上冲浪吗3 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
△曉風殘月〆4 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试