553C++笔试笔记2014

目录

1.递归

2.整型数转换成字符串

补充1:itoa()

 补充2:c_str()

3.交换数组前m项

总结一下:

4.Date类和Employee类


1.递归

#include<iostream>
using namespace std;

int ack(int m, int n) {
	if (m == 0)
		return n + 1;
	else
		if (n == 0)
			return m + 1;
		else
			return ack(m - 1, n - 1);

}

int main() {
	cout << ack(34, 41);
}

2.整型数转换成字符串

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

void IntToStr(int a, char s[]) {
	stack<int> stack;
	int count = 0;
	while (a) {
		stack.push(a % 10);
		a = a / 10;
		count++;
	}
	for (int i = 0; i < count; i++) {
		s[i] = stack.top()+'0';        //int+'\0'是int对应的数字字符
		stack.pop();
	}
	s[count] = '\0';
}

int main() {
	int a = 1123273;
	char s[8];        //一开始我写的s[7],没留位置给'\0'
	IntToStr(a, s);
	cout << s << endl;
	return 0;
}

int+'0'表示int型对应的字符的asc码,即int型对应的字符,从int到char的转换可以靠该方法实现。

补充1:itoa()

复看:介绍另一种函数

int main() {
	int a = 1123273;
	char s[8];

	_itoa_s(a, s,10);  
	cout << s<<endl;

	IntToStr(a, s);   //我们写的
	cout << s << endl;
	return 0;
}
1123273
1123273

 _itoa_s的源头是c函数itoa(),其有三个参数:

char *  itoa ( int value, char * buffer, int radix );

前两个就不赘述,分别是输入和输出;第三个是基数,即这个函数封装了进制转换radix在2-36之间。_itoa_s, 是 itoa 的安全版本,除了参数和返回值不同,两个函数的行为是相同的,都是将整数转换为字符串。 

 补充2:c_str()

const cahr* c_str(string s);

将字符串类型转换为字符数组。

3.交换数组前m项

思路就是先分别颠倒前n项与后m项,再将整个数组颠倒。

#include<iostream>
using namespace std;

//颠倒前n个元素与后n个元素,最后将整个数组颠倒
//这里默认数组长度为m+n

void exchange(int a[],int left,int right) {
	int temp;
	while (left < right) {
		temp = a[left];
		a[left] = a[right];
		a[right] = temp;
		left++;
		right--;
	}
}
void swap(int a[], int m, int n) {
	exchange(a, 0, m - 1);
	exchange(a, m , m + n - 1);
	exchange(a, 0, m + n - 1);
}
int main() {
	int a[10] = { 1,2,3,4,5,6,7,3,4,5 };
	swap(a, 4, 6);
	for (auto const item : a)
		cout << item << " ";
	 
}

总结一下:

对于字符数组,可以这么颠倒数组

#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char s[]="hello";
 
    strrev(s);
 
    cout<<s<<endl;
 
    return 0;
}

对于字符串,可以这样:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
 
int main()
{
    string s = "hello";
 
    reverse(s.begin(),s.end());
 
    cout<<s<<endl;
 
    return 0;
}

 但这题里,是要部分数组颠倒,不太方便,所以不如自己写。

其实感觉也可以空间换时间,申请一个队列,时间原因,交给大家去实现。

4.Date类和Employee类

都要具备构造函数,设置和打印函数,其中date对象将作为Employee对象的数据成员。

#include<iostream>
#include <iomanip>
using namespace std;
class Date {
private:
	int year;
	int month;
	int day;
public:
	Date(int y,int m,int d) {
		year = y;
		month = m;
		day = d;
	}
	void setDate(int y, int m, int d) {
		year=y;
		month = m;
		day = d;
	}
	int getYear() const{
		return year;
	}
	void setYear(int y) {
		year = y;
	}
	void print() const{
		cout << year << "." << month << "." << day;
	}
};

class Employee {
private:
	int Enum;
	string name;
	int ID;
	Date birthday;
	Date Eday;
	Date Et;
	double salary;
public:
	Employee(int en, string n,int i,Date b,Date e,Date et,double s):Enum(en),name(n),ID(i),birthday(b),Eday(e),Et(et),salary(s){}
	void setBirthday(int y,int m,int d) {
		birthday.setDate(y, m, d);
	}
	void changeEt(Date d) {
		Et = d;
	}
	void changeSalary(int s) {
		salary = s;
	}
	void continueEngage(int t) {
		if (Et.getYear() + t - birthday.getYear() >= 60)
			cout << "不能续聘!";
		else {
			Et.setYear(Et.getYear()+t);
			cout << "续聘成功!";
		}
	}
	void display()const {
		cout << "工号:" << Enum << endl << "姓名:" << name << endl << "身份证号:" << ID << endl;
		cout << "出生年月:";
		birthday.print();
		cout << endl << "聘用年限:" << Et.getYear();
		cout << endl << "年薪:" << fixed<< setprecision(0)<< salary;
	}
};

int main() {
	Date birthday(2001, 4, 15);
	Date Eday(2023, 4, 15);
	Date Et(10, 0, 0);

	Employee yj(00, "yj", 3206822, birthday, Eday, Et, 20000000);

	yj.display();

	yj.continueEngage(5);
	yj.display();
	return 0;
}

唯一值得注意的是,c++在输出比较大的数字的时候,会用科学计数法输出,如果想要正常显示的话,需要用到iomanip库里的fixed。再借助setprecision控制浮点数字的个数,就可以直观显示数字。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值