![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
MOOC PKU 程序设计与算法(三)C++面向对象程序设计
uflyjz
小白
展开
-
043:冷血格斗场
描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。我们假设格斗的实力可以用一个非负整数表示,称为实力值,两人的实力值可以相同。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有多个人的实力值与他差别相同,则他会选择id最小的那个。不幸的是,Facer一不小.原创 2021-08-27 17:45:37 · 135 阅读 · 0 评论 -
042:热血格斗场
描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家热血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。我们假设格斗的实力可以用一个正整数表示,成为实力值。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有两个人的实力值与他差别相同,则他会选择比他弱的那个(显然,虐人必被虐好)。不幸的是,Facer一不小心把.原创 2021-08-27 16:29:31 · 216 阅读 · 0 评论 -
041:Set
描述现有一整数集(允许有重复元素),初始为空。我们定义如下操作:add x 把x加入集合del x 把集合中所有与x相等的元素删除ask x 对集合中元素x的情况询问对每种操作,我们要求进行如下输出。add 输出操作后集合中x的个数del 输出操作前集合中x的个数ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。输入第一行是一个整数n,表示命令数。0<=n<=100000。后面n行命令,如Description中所述。.原创 2021-08-27 14:22:29 · 208 阅读 · 0 评论 -
040:List
描述写一个程序完成以下命令:new id ——新建一个指定编号为id的序列(id<10000)add id num——向编号为id的序列加入整数nummerge id1 id2——合并序列id1和id2中的数,并将id2清空unique id——去掉序列id中重复的元素out id ——从小到大输出编号为id的序列中的元素,以空格隔开输入第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。输出按题目要求输出。样例输入16new 1new 2.原创 2021-08-26 23:06:39 · 164 阅读 · 0 评论 -
039:我自己的 ostream_iterator
描述程序填空输出指定结果#include <iostream>#include <list>#include <string>using namespace std;template <class T1,class T2>void Copy(T1 s,T1 e, T2 x){ for(; s != e; ++s,++x) *x = *s;} template<class T>class myostream_it.原创 2021-08-26 22:39:36 · 418 阅读 · 1 评论 -
038:白给的list排序
描述程序填空,产生指定输出#include <cstdio>#include <iostream>#include <algorithm>#include <list>using namespace std;int main(){ double a[] = {1.2,3.4,9.8,7.3,2.6}; list<double> lst(a,a+5); lst.sort(// 在此处补充你的代码); for(li.原创 2021-08-26 22:04:57 · 645 阅读 · 0 评论 -
037:函数对象的过滤器
描述程序填空输出指定结果#include <iostream>#include <vector>using namespace std;struct A { int v; A() { } A(int n):v(n) { }; bool operator<(const A & a) const { return v < a.v; }};// 在此处补充你的代码template <class T>void Print.原创 2021-08-26 21:45:32 · 148 阅读 · 0 评论 -
036:很难蒙混过关的CArray3d三维数组模板类
描述实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,输出指定结果#include <iostream>#include <iomanip> #include <cstring>using namespace std;template <class T>class CArray3D{// 在此处补充你的代码};CArray3D<int> a(3,4,5);CArray3D<doubl.原创 2021-08-26 18:44:46 · 307 阅读 · 0 评论 -
035:按距离排序
描述程序填空,输出指定结果#include <iostream>#include <cmath>#include <algorithm>#include <string>using namespace std;template <class T1,class T2>struct Closer {// 在此处补充你的代码};int Distance1(int n1,int n2) { return abs(n1-n2);.原创 2021-08-26 17:23:30 · 201 阅读 · 0 评论 -
034:goodcopy
描述编写GoodCopy类模板,使得程序按指定方式输出#include <iostream>using namespace std;template <class T>struct GoodCopy {// 在此处补充你的代码};int a[200];int b[200];string c[200];string d[200];template <class T>void Print(T s,T e) { for(; s != e;.原创 2021-08-26 16:26:46 · 216 阅读 · 0 评论 -
033:排序,又见排序!
描述自己编写一个能对任何类型的数组进行排序的mysort函数模版。只能写一个mysort模板,不能写mysort函数!#include <iostream>using namespace std;bool Greater2(int n1,int n2) { return n1 > n2;}bool Greater1(int n1,int n2) { return n1 < n2;}bool Greater3(double d1,double d2){.原创 2021-08-26 15:55:40 · 149 阅读 · 0 评论 -
032:这个模板并不难
描述程序填空,输出指定结果#include <iostream>#include <string>#include <cstring>using namespace std;template <class T> class myclass {// 在此处补充你的代码~myclass( ) { delete [] p; } void Show() { for( int i = 0;i < size;i ++ ) { .原创 2021-08-24 22:20:37 · 189 阅读 · 0 评论 -
031:山寨版istream_iterator
描述模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出#include <iostream>#include <string>using namespace std;template <class T>class CMyistream_iterator{// 在此处补充你的代码};int main() { int t; cin >> t; while.原创 2021-08-24 21:40:34 · 441 阅读 · 0 评论 -
030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?
描述读入两个整数,输出两个整数 ,直到碰到-1#include <iostream>using namespace std;class MyCin{// 在此处补充你的代码};int main(){ MyCin m; int n1,n2; while( m >> n1 >> n2) cout << n1 << " " << n2 << endl; re.原创 2021-08-24 20:47:24 · 312 阅读 · 0 评论 -
029:简单的Filter
描述编写Filter模板,使得程序产生指定输出 不得编写 Filter函数#include <iostream>#include <string>using namespace std;// 在此处补充你的代码bool LargerThan2(int n){ return n > 2;}bool LongerThan3(string s) { return s.length() > 3;}string as1[5] = {"Tom","M.原创 2021-08-24 20:10:26 · 201 阅读 · 0 评论 -
028:简单的foreach
描述编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数#include <iostream>#include <string>using namespace std;// 在此处补充你的代码void Print(string s){ cout << s;}void Inc(int & n){ ++ n;}string array[100];int a[100];int main() { int m,.原创 2021-08-24 19:36:23 · 230 阅读 · 0 评论 -
027:简单的SumArray
描述填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数#include <iostream>#include <string>using namespace std;template <class T>T SumArray(// 在此处补充你的代码}int main() { string array[4] = { "Tom","Jack","Mary","John"}; cou.原创 2021-08-24 18:56:19 · 313 阅读 · 0 评论 -
026:编程填空:统计动物数量
描述代码填空,使得程序能够自动统计当前各种动物的数量#include <iostream>using namespace std;// 在此处补充你的代码void print() { cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of t.原创 2021-08-24 14:21:05 · 247 阅读 · 0 评论 -
025:怎么又是Fun和Do
描述程序填空输出指定结果#include <iostream>using namespace std;class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; virtual void Do() { cout << "A::Do" << endl; }};class B:public A { public: virt.原创 2021-08-24 13:33:34 · 122 阅读 · 0 评论 -
024:这是什么鬼delete
描述程序填空输出指定结果#include <iostream> using namespace std;class A { public: A() { }// 在此处补充你的代码}; class B:public A { public: ~B() { cout << "destructor B" << endl; } }; int main() { A * pa; pa = new B; delete pa; retu.原创 2021-08-24 12:20:51 · 139 阅读 · 0 评论 -
023:Fun和Do
描述程序填空输出指定结果#include <iostream> using namespace std;class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; void Do() { cout << "A::Do" << endl; } }; class B:public A { public: .原创 2021-08-24 12:10:29 · 310 阅读 · 0 评论 -
022:看上去像多态
描述程序填空产生指定输出#include <iostream>using namespace std;class B { private: int nBVal; public: void Print() { cout << "nBVal="<< nBVal << endl; } void Fun() {cout << "B::Fun" << endl; } B ( int n ) .原创 2021-08-22 08:50:46 · 84 阅读 · 0 评论 -
020:继承自string的MyString
描述程序填空,输出指定结果#include <cstdlib>#include <iostream>#include <string>#include <algorithm>using namespace std;class MyString:public string{// 在此处补充你的代码};int main(){ MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString .原创 2021-08-22 08:07:39 · 157 阅读 · 0 评论 -
018:别叫,这个大整数已经很简化了!
总时间限制: 1000ms 内存限制: 65536kB描述程序填空,输出指定结果#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std;const int MAX = 110; class CHugeInt {// 在此处补充你的代码};int main() { char s[210]; .原创 2021-08-22 08:07:28 · 238 阅读 · 0 评论 -
017:二维数组类
总时间限制: 1000ms 内存限制: 65536kB描述写一个二维数组类 Array2,使得下面程序的输出结果是:0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,程序:#include <iostream>#include <cstring>using namespace std;class Array2 {// 在此处补充你的代码};int main() {.原创 2021-08-22 08:07:07 · 759 阅读 · 0 评论 -
016:惊呆!Point竟然能这样输入输出
总时间限制: 1000ms 内存限制: 65536kB描述程序填空#include <iostream> using namespace std;class Point { private: int x; int y; public: Point() { };// 在此处补充你的代码}; int main() { Point p; while(cin >> p) { cout << p << end.原创 2021-08-19 10:46:59 · 115 阅读 · 0 评论 -
015:看上去好坑的运算符重载
015:看上去好坑的运算符重载查看 提交 统计 提问总时间限制: 1000ms 内存限制: 65536kB描述程序填空#include <iostream> using namespace std;class MyInt { int nVal; public: MyInt( int n) { nVal = n ;}// 在此处补充你的代码}; int Inc(int n) { return n + 1;}int main () { int n; w.原创 2021-08-19 10:20:02 · 374 阅读 · 0 评论 -
014:MyString
总时间限制: 1000ms 内存限制: 65536kB描述补足MyString类,使程序输出指定结果#include <iostream>#include <string>#include <cstring>using namespace std;class MyString { char * p;public: MyString(const char * s) { if( s) { p = new char[strlen(s) + 1];.原创 2021-08-19 09:31:58 · 258 阅读 · 0 评论 -
有关类型转换构造函数和‘=’运算符重载——一次debug总结
#include <iostream>#include <string>#include <cstring>using namespace std;class MyString{ char *p;public: MyString(const char *s) { if (s) { p = new char[strlen(s) + 1]; strcpy(p.原创 2021-08-19 08:55:08 · 115 阅读 · 0 评论 -
012:这个指针哪来的
总时间限制: 1000ms 内存限制: 65536kB描述填空,按要求输出#include <iostream>using namespace std;struct A{ int v; A(int vv):v(vv) { }// 在此处补充你的代码};int main(){ const A a(10); const A * p = a.getPointer(); cout << p->v << endl; return 0;.原创 2021-08-18 21:02:30 · 137 阅读 · 0 评论 -
011:Big & Base 封闭类问题
总时间限制: 1000ms 内存限制: 65536kB描述程序填空,输出指定结果#include <iostream>#include <string>using namespace std;class Base {public: int k; Base(int n):k(n) { }};class Big{public: int v; Base b;// 在此处补充你的代码};int main(){ int n; while(cin &.原创 2021-08-17 17:49:26 · 92 阅读 · 0 评论 -
010:返回什么才好呢
总时间限制: 1000ms 内存限制: 65536kB描述程序填空,使其按要求输出#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码};int main(){ int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj(.原创 2021-08-17 17:35:56 · 87 阅读 · 0 评论 -
this指针的存放
1) 以下说法不正确的是:A.B) this指针就是指向成员函数所作用的对象的指针B.D) 类的非静态成员函数,真实的参数比所写的参数多1C.A) 静态成员函数中不能使用this指针D.C) 每个对象的空间中都存放着一个this指针ans:D假设用一个类定义10个对象:编译系统并不是分别为10个对象的数据成员和成员函数分配存储单元,而仅为每个对象的数据成员分配存储单元,10个对象的成员函数对应的是同一个函数代码段。不论成员函数在类内定义还是在类外定义,成员函数都用以.原创 2021-08-17 17:23:14 · 489 阅读 · 0 评论 -
009:哪来的输出
总时间限制: 1000ms 内存限制: 65536kB描述程序填空,输出指定结果 #include <iostream>using namespace std;class A { public: int i; A(int x) { i = x; }// 在此处补充你的代码};int main(){ A a(1); A * pa = new A(2); delete pa; return 0;}输入无输出21样例输入无样例输出21来源.原创 2021-08-17 14:23:17 · 67 阅读 · 0 评论 -
008:超简单的复数类
总时间限制: 1000ms 内存限制: 65536kB描述下面程序的输出是:3+4i 5+6i请补足Complex类的成员函数。不能加成员变量。#include <iostream>#include <cstring>#include <cstdlib>using namespace std;class Complex {private: double r,i;public: void Print() { c.原创 2021-08-17 12:25:59 · 286 阅读 · 0 评论 -
007:Program Completion
总时间限制: 1000ms 内存限制: 65536kB描述Please complete the following program according to the specified output.#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码};int main(){ int m,n; A a; cout << a.val &l.原创 2021-08-17 11:59:10 · 102 阅读 · 0 评论 -
006:奇怪的类复制
总时间限制:1000ms内存限制:65536kB// 在此处补充你的代码描述程序填空,使其输出9 22 5#include <iostream>using namespace std;class Sample {public: int v;};void PrintAndDouble(Sample o){ cout << o.v; cout << endl;}int main(){ Sample a(5)..原创 2021-08-17 11:06:33 · 221 阅读 · 0 评论 -
005:编程填空:学生信息处理程序
总时间限制:1000ms内存限制:1024kB// 在此处补充你的代码描述实现一个学生信息处理程序,计算一个学生的四年平均成绩。要求实现一个代表学生的类,并且类中所有成员变量都是【私有的】。补充下列程序中的 Student 类以实现上述功能。#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <..原创 2021-08-16 23:02:51 · 985 阅读 · 0 评论 -
析构函数调用时机
假设A是一个类的名字,下面的程序片段会类A的调用析构函数几次?int main() {A * p = new A[2];A * p2 = new A;A a;delete [] p;} A. A) 1 B. B) 2 C. C) 3 D. D) 4 答案:c解析:new出来的对象只有delete才会消亡,否则可以认为直到main函数结束了也不消亡。...原创 2021-08-16 20:39:22 · 1562 阅读 · 1 评论 -
004:神秘的数组初始化
总时间限制:1000ms内存限制:65536kB// 在此处补充你的代码描述填空,使得程序输出指定结果#include <iostream>using namespace std;int main(){ int * a[] = {}; *a[2] = 123; a[3][5] = 456; if(! a[0] ) { cout << * a[2] << "," << a[3][5]; }..原创 2021-08-16 15:48:15 · 146 阅读 · 0 评论