EX 9

news/2025/3/20 22:36:11/

优于别人,并不高贵,真正的高贵,应该是优于过去的自己。

                          —— 海明威 《真实的高贵》

ps:算是手速场吧,没有像上场那么自闭,看来喊喊rank1还是有用的,嘻嘻。

 

Problem A: 第一个类

main函数:

int main()
{Thing A("Car");string str;cin>>str;Thing B(str);return 0;
}

水。。。。。。。。

AC代码:

#include <bits/stdc++.h>
using namespace std;
struct Thing
{string name;
public:Thing(string _name):name(_name){cout << "Construct a thing " << name << endl; }~Thing()
{cout << "Destroy a thing " << name << endl;
}
};

Problem B: 建造一间教室

main函数:

int main()
{int nl, nc;int w;string color;cin>>w>>color;Light light(w);Chair chair(color);cin>>nl>>nc;cin>>w>>color;ClassRoom room(nl, nc, w, color);return 0;
}

半水吧,就一个坑点,chair类的析构也输出created,还好测了遍样例,手动狗头。

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Light
{int w;
public:Light(int _w):w(_w){printf("A %dw light is created.\n",w);}~Light(){printf("A %dw light is erased.\n",w);}
};
class Chair
{string co;
public:Chair(string _co):co(_co){cout << "A " << co << " chair is created." << endl;}~Chair(){cout << "A " << co << " chair is created." << endl;}
};
class ClassRoom
{Light li;Chair ch;int n_c;int n_l;
public:ClassRoom(int _c,int _l,int _li,string _ch):n_c(_c),n_l(_l),li(_li),ch(_ch){printf("A classroom having %d lights and %d chairs is created.\n",n_c,n_l);}~ClassRoom(){printf("A classroom having %d lights and %d chairs is erased.\n",n_c,n_l);}
};

Problem C: 是否回文数?

main函数:

int main()
{Data data;int v;while (cin>>v){data.setValue(v);if (data.isPalindrome())cout<<"Yes"<<endl;elsecout<<"No"<<endl;}return 0;
}

重点应该是在isPalindrome函数的设计的吧,基本思路应该是先取个abs,然后转化为字符串看是否对称的吧。

way1 : 使用sprintf 函数将数字转化为字符串 sprintf(要转入的字符数组s,格式控制符,转出的数字);

way2: 使用STL库中的反转函数 reverse(s.begin(),s.end());

ac代码:

#include <bits/stdc++.h>
using namespace std;
class Data
{int v;
public:Data(){}void setValue(int x){v =x;}bool isPalindrome()// way 1
    {int d = v;d = abs(d);char s[20];sprintf(s,"%d",d);int len = strlen(s);for(int i = 0,j=len-1;i<=j;i++,j--)if(s[i]!=s[j])return false;return true;}
bool isPalindrome()// way 2
{int d = abs(v);string s,s1;while(d){s+=(d%10+'0');d/=10;}s1 = s;int len = s.size();reverse(s1.begin(),s1.end());for(int i = 0;i<=(len+1)/2;i++)if(s[i]!=s1[i])return false;return true;
}
};

Problem D: Base与Derived

main函数:

int main()
{int a, b;cin>>a>>b;Base base(a);Derived derived(a, b);return 0;
}

 水,细节题,析构输出created 。。。。

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Base
{int b;
public:Base(int _b):b(_b){printf("Base %d is created.\n",b);}~Base(){printf("Base %d is created.\n",b);}
};
class Derived:public Base
{int d;
public:Derived(int _b,int _d):Base(_b),d(_d){printf("Derived %d is created.\n",d);};~Derived (){printf("Derived %d is created.\n",d);}
};

Problem E: 类模板Sample

main函数:

int main()
{int a, b;double c, d;cin>>a>>b>>c>>d;Sample<int> s1(a), s2(b), s3(s1);Sample<double> s4(c), s5(d), s6(s5);s1.add(s2);s1.show();s5.add(s4);s5.show();return 0;
}

类模板

AC代码:

#include <bits/stdc++.h>
using namespace std;
template <class T>
class Sample
{
T num;
public:
Sample(T _num):num(_num)
{
cout << "Sample " << num << " is created." << endl;
}
template <class T1>
Sample(Sample<T1> &b)
{
num = b.num;
cout << "Sample " << num << " is copied." << endl;
}
void show()
{
cout << num << endl;
}
template <class T1>
void add(Sample<T1> b)
{
num+=b.num;
}
};

 

转载于:https://www.cnblogs.com/baihualiaoluan/p/10962312.html


http://www.ppmy.cn/news/302386.html

相关文章

上海车展:NVIDIA携“自动驾驶朋友圈”展示智能出行

上海再一次印证了“魔都”之名——本周&#xff0c;来自20个国家和地区1,000多家展商齐聚&#xff0c;参加备受期待的第二十届上海国际汽车工业展览会&#xff08;以下简称“上海车展”&#xff09;。 今年上海车展预计展出整车约1,500辆&#xff0c;集中展示AI赋能的最新车型以…

激光雷达上车「热」背后的焦虑

激光雷达的上车节奏正在加速。 高工智能汽车研究院监测数据显示&#xff0c;从2022年9月至今&#xff0c;中国市场乘用车月度前装标配搭载激光雷达一直保持在1.5万台以上&#xff0c;其中&#xff0c;去年12月更是单月冲破3万台大关。 本周&#xff0c;Luminar宣布扩大与梅赛德…

Express-02

本节&#xff1a;学习数据库的搭建和数据库的基本语法。 一、创建数据库 (1) 下载数据库可视化管理软件&#xff1a;heidisql &#xff08;2&#xff09;创建数据库会话&#xff1a;网络类型为sqllite 数据库文件名&#xff1a;选择数据库位置了。&#xff08;创建…

extjs02

Ext.js 自定义事件和监听器 2022-05-20 17:11 更新 事件是在类发生的时候触发的。 例如&#xff0c;当一个按钮被点击或元素被渲染之前/之后。 写事件的方法: 内置事件使用侦听器附加事件监听自定义事件 内置事件使用侦听器 xt JS提供了用于在Ext JS文件中编写事件和自定…

激光雷达行业进入动荡期,中国市场注入「确定性」

"每辆车都配备的安全气囊&#xff0c;全球主要由两家供应商提供&#xff0c;而ADAS相关传感器、系统以及底盘控制类部件&#xff0c;也是如此……任何与功能安全有关的&#xff0c;往往主要的市场份额都集中在少数几家主要供应商。”这是激光雷达公司Innoviz首席执行官Ome…

哪些图商在真正交付高精度地图?数据驱动释放红利

对于高阶智能驾驶&#xff0c;尤其是高速及城区导航辅助驾驶来说&#xff0c;高精度地图&#xff08;目前图商定义为高级辅助驾驶地图&#xff09;的重要性仍然毋庸置疑。 今年3月&#xff0c;四维图新宣布&#xff0c;公司收到国内车厂长城汽车股份有限公司重庆采购分公司发出…

开启激光雷达规模交付!禾赛再度冲刺IPO,竞争白热化下的隐忧

激光雷达上市公司阵营&#xff0c;即将迎来第一家中国公司。 本周&#xff08;美国时间2月2日&#xff09;&#xff0c;禾赛科技正式对外披露IPO计划&#xff0c;预计将在美股发行900万股美国存托凭证&#xff08;ADS&#xff09;&#xff0c;首次公开发行价格将在17美元至19美…

激光雷达+4D环绕成像雷达,新一轮「感知」军备竞赛开打

1个LUMINAR 1550nm激光雷达、2个采埃孚的Premium 4D成像雷达、4个来自海拉的增强版远距离点云角雷达&#xff0c;即将上市交付的上汽旗下子品牌飞凡R7率先在国内市场掀起第一波「激光雷达4D环绕成像点云毫米波雷达」全新感知组合潮流。 今年开始&#xff0c;大部分车企将纯视觉…