• 2014


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

    1. #include<iostream>
    2. using namespace std;
    3. int A(int m,int n){
    4. if(m==0){
    5. return n+1;
    6. }
    7. else if(m>0&&n==0){
    8. return A(m-1,1);
    9. }
    10. else{
    11. return A(m-1,A(m,n-1));
    12. }
    13. }
    14. int main(){
    15. int m,n;
    16. cout<<"please input two number"<<endl;
    17. cin>>m>>n;
    18. cout<<A(m,n)<<endl;
    19. return 0;
    20. }

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

    1. //法一
    2. #include<iostream>
    3. #include<string>
    4. #include<sstream>
    5. using namespace std;
    6. string intToString(int num)
    7. {
    8. stringstream s;
    9. s<<num;
    10. string str;
    11. s>>str;
    12. return str;
    13. }
    14. int main(){
    15. cout<<intToString(3)<<endl;
    16. return 0;
    17. }
    1. //法二
    2. #include<iostream>
    3. #include<string>
    4. #include<sstream>
    5. using namespace std;
    6. string intToString(int num)
    7. {
    8. string r =" ";
    9. while(num){
    10. int n =num%10;
    11. r =char('0'+n)+r;//将数字转换成字符拼接到r前面
    12. num =num/10;
    13. }
    14. return r;
    15. }
    16. int main(){
    17. cout<<intToString(15)<<endl;
    18. return 0;
    19. }

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

    1. //写一个 swap(int a[], int m, int n )
    2. //使得数组的前 m 项和后 n 项交换位置
    3. #include<iostream>
    4. using namespace std;
    5. void reverse(int a[],int m,int n){
    6. while(m<n){
    7. int temp = a[m];
    8. a[m] = a[n];
    9. a[n] = temp;
    10. m++;
    11. n--;
    12. }
    13. }
    14. void swap(int a[],int m,int n,int k){
    15. reverse(a,0,m-1);//反转前m个数
    16. reverse(a,m,k-n-1);//反转中间的数
    17. reverse(a,k-n,k-1);//反转后n个数
    18. reverse(a,0,k-1);//反转整个数组
    19. }
    20. int main(){
    21. int a[]={1,2,3,5,6,7};
    22. int k= 6;
    23. swap(a,2,2,k);
    24. for(int i=0;i<k;i++){
    25. cout<<a[i]<<" ";
    26. }
    27. return 0;
    28. }

    4, 

    1. //1)写一个日期 Date 类,成员有年月日,成员函数有无参数构造函数、设置年月日的函数 setDate,还有一个打印函数 display
    2. //2)第二个类是员工类 Employee,成员有工号、姓名、身份证号、出生日期、受聘日期、聘用年限、月薪
    3. //成员函数要有构造函数、改变出生日期函数、改变聘用年限函数、改变月薪函数、续聘函数(要求当续聘后的年龄大于 60 时给提示不能续聘)
    4. //还有展示函数 display,需要有工号、姓名、身份证号、出生日期、聘用到期时间、聘用年限、年薪,注意第二个类会有 Date 类或其指针作为成员
    5. #include<iostream>
    6. #include<string>
    7. using namespace std;
    8. class Date{
    9. public:
    10. Date(int y=0,int m= 0,int d= 0):year(y),month(m),day(d){}
    11. Date(Date &d){
    12. this->year = d.year;
    13. this->month =d.month;
    14. this->day =d.day ;
    15. }
    16. void setDate(int y,int m,int d){
    17. year = y;
    18. month = m;
    19. day = d;
    20. }
    21. int getyear() const{
    22. return year;
    23. }
    24. int getmonth()const{
    25. return month;
    26. }
    27. int getday() const{
    28. return day;
    29. }
    30. void display()const{
    31. cout<<year<<"-"<<month<<"-"<<day;
    32. }
    33. private:
    34. int year;
    35. int month;
    36. int day;
    37. };
    38. class Employee{
    39. public:
    40. Employee(long id,string name,long number,Date birthday,Date workday,int workyear,double salary){
    41. this->id = id;
    42. this->name = name;
    43. this->number = number;
    44. this->number = number;
    45. this->birthday = birthday;
    46. this->workday = workday;
    47. this->workyear = workyear;
    48. this->salary = salary;
    49. }
    50. void changeBirthday(Date newdate){
    51. birthday = newdate;
    52. }
    53. void changeworkyear(int newyear){
    54. workyear = newyear;
    55. }
    56. void changesalary(double newsalary){
    57. salary = newsalary;
    58. }
    59. void renewContract(int time) { // 续聘
    60. if (birthday.getyear()+time > 60){
    61. cout<<"续聘不成功"<<endl;
    62. }
    63. else
    64. {
    65. workyear+=time;
    66. }
    67. }
    68. //还有展示函数 display,需要有工号、姓名、身份证号、出生日期、聘用到期时间、聘用年限、年薪
    69. void display()const{
    70. cout << "工号:" << id << " 姓名:" << name << " 身份证号:" << number << " 出生日期:";
    71. birthday.display();
    72. cout<<"聘用到期时间"<<workday.getyear() +workyear<<"年"<<workday.getmonth()<<"月"<<workday.getday() <<"日";
    73. cout<<"聘用年限"<<workyear<<"年薪"<<salary<<endl;
    74. }
    75. private:
    76. long id;
    77. string name;
    78. long number;
    79. Date birthday;
    80. Date workday;
    81. int workyear;
    82. double salary;
    83. };
    84. int main(){
    85. Date birth(1990, 10, 25); // 创建出生日期对象
    86. Date hire(2020, 5, 15); // 创建聘用日期对象
    87. Employee emp(1001, "John Doe", 1234567890, birth, hire, 3, 5000.0); // 创建员工对象
    88. emp.display(); //
    89. emp.renewContract(20);
    90. return 0;
    91. }

     

  • 相关阅读:
    SQL中的正则使用
    R2DBC正式孵化成功,利好Spring Webflux
    工作中Java Stream的简单应用
    JS中ES5和ES6的区别
    2023工博会 | 上海添力网络营销公司 | 助力工业品线上推广
    台积电嘲讽英特尔CEO:不可能超越我们了,安心退休吧
    1006 换个格式输出整数【PAT (Basic Level) Practice (中文)】
    双位置继电器DLS-42/6-4/DC110V
    【Python自动化】Python-docx基础入门--插入table表格样式设置
    【使用 BERT 的问答系统】第 3 章 :词嵌入介绍
  • 原文地址:https://blog.csdn.net/qq_62319385/article/details/136661158