SDII_FUN

  • 期末三套卷部分题解
  • sdii 模板
  • 期末
    • 机考附加题(图论算法/贪心)
    • 笔试笔记

期末考试三套模拟卷

  • 12计科T2,9,14,error
  • 09软工T4,T7
  • 10软工T3,T6
  • 记不清题号的笔记
  • 顺便作答疑帖0.0有问题可以在下方留言

12计科

T2

Which of the following about constructor is not correct?

B) Copy constructor will not be created by the compiler if a default constructor is defined.

D) A class has at least two constructors.

参考资料: (B)(又如拷贝构造函数会覆盖默认,认为D也正确)

复杂理论

The Copy Constructor will not be implicitly generated if

  • you have explicitly declared a copy constructor (for class X a constructor taking X, X& or const X& )
  • there is a member in your class that is not copy-constructible (such as a class with no or inaccessible copy constructor)
  • (C++11) you have explicitly told the compiler to not generate one using A(const A&) = delete;

The Default Constuctor will not be implicitly generated if

  • you have explicitly declared any constructor at all
  • There is a member in your class that is not default-constructible (such as a reference, a const object, or a class with no or inaccessible default constructor)
  • (C++11) you have explicitly told the compiler to not generate one using A() = delete;
  • B: 因为定义了一个默认构造,如果不是 taking X, X& or const X& 那么编译器还是会隐式实现
  • D: 尽管按照上面讲的不能被隐式实现,但还是要求被显式实现
  • 另外这是12年的卷子= = 应该不包括C++11特性

简明题解 : 就是反正compiler会给搞一个default constructor 一个copy constructor

T9

With the base class and its inherited class, which of the following statement has a compilation error?

A) Base * base = Inherited();

C) Base base = Inherited();

  • A 对象不能初始化指针
  • 另外 Base * base = &Inherited();也是错的,因为不能把const强转成非const
    • 也就是说类名(参数)这样直接声明的对象是const

T14

C++中,通过基类的引用或指针调用虚函数时,发生动态绑定。

Error Find

  1. 普通对象也可以调用static函数

  2. variable, static function cannot be virtual

  3. static function cannot be const

  4. 类声明完成后需要加’;’

    1
    2
    3
    class Base
    {
    };
  5. delete不是new 出来的空间会RE,

  6. =0纯虚不能被=false代替

  7. 抽象类不能被实例化(在子类里面)不能 声明一个虚基类对象 Base(1)

  8. 不能deletereference

  9. 不是static 不能在类外 class::fun()调用

  10. add完char[]后最后一位补0

  11. 重载了(char *)≠重载(const char *)

09软工

(观察代码题)

T4虚继承

  • 这道题中是虚继承,所以只生成一次A的拷贝

(下面解析代码来自wiki)

假如类A和类B各自从类X派生(非虚继承且假设类X包含一些数据成员),且类C同时多继承自类AB,那么C的对象就会拥有两套X的实例数据(可分别独立访问,一般要用适当的消歧义限定符)。但是如果类AB**各自*虚继承了类X,那么C的对象就只包含一套类X*的实例数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Animal {
public:
virtual void eat();
};

class Mammal : public Animal {
public:
virtual void breathe();
};

class WingedAnimal : public Animal {
public:
virtual void flap();
};

// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {
};

//爷爷类指针调用
Bat b;
Animal &mammal = static_cast<Mammal&> (b);
Animal &winged = static_cast<WingedAnimal&> (b);

为了正确的调用eat(),还需要相同的可以消歧义的语句:static_cast(bat).eat()static_cast(bat).eat().

T7注意是每次插入起始位置

10软工

T3 reference

维基百科说的很清楚),大意是reference是一个封装的更加安全(指针派生)的pointer

T6

sizeof(classname)不包括static大小

电脑没电后的笔记

  1. 调用一堆初始化函数后初始化时从右到左

  2. static函数是父类和子类共用的

  3. 三金关于01010的博客

    1. 主要思想是因为是base指针,它++的步长也就是base的大小

    2. 另外Post-increment (++) has higher precedence than dereference (*).

      When you increment a T*, it moves sizeof(T) bytes.† This is because it doesn’t make sense to move any other value: if I’m pointing at an int that’s 4 bytes in size, for example, what would incrementing less than 4 leave me with? A partial int mixed with some other data: nonsensical.

      stackoverflow

软件设计II模板

点击这里下载压缩包

文件说明:

6.11为软设模板,基本能符合类的设计需要

STL学习笔记为下一篇日志的pdf可打印版本,里面是对STL重要容器算法的模板

ACM模板是我花了一定精力从网上收集的很多算法模板,应大家需求共享出来

(不过这样会增加一定的流量费用)

sdii/CPP文件夹内有一些函数库的cplusplus主页和软设模板的md源文件,可以自行修改

点击这里进入项目主页愿意为我star的当然欢迎XDD
有疑问和建议联系这个邮箱
leidar100@gmail.com

期末

期末附加题

  • 给一棵树,问能不能两两节点配对完
  • 链接,题号18381,直接主页submit

AC的方法

  • 优先队列-镓伟
  • n是奇数 non是偶数 原来的树如果有一个点连着多于1个1度点 no否则 yes —颂恒
  • 直接bfs..不需要堆优化-诗源
  • dfs -leida
  • 考试的时候最后三分钟才意识到树形DP…
  • 下面这段代码不一定能过=_=理论AC吧XD(已经AC)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <vector>
#include <cstdio>
using namespace std;

const int maxn = 10000 + 5;
vector<int> e[maxn];
int paired[maxn];//存储是否配对成功
int n,a,b,flag;//个数,两个临时变量,YES

void dfs(const int pos,const int fa)
{

if (flag == 0) return;//错误状态
for (int i = 0; i < e[pos].size(); i++) {
int nt = e[pos][i];
if (nt != fa) {
dfs(nt, pos);//测试下一个配对情况
if (flag == 0) return;
}
}
if (paired[pos] == false) {
if (fa < 0 || paired[fa] == 1 ) {
flag = 0;//已经被配对
}else
paired[fa] = paired[pos] = 1;
}//没有被子树的节点需要配对
}
int main()
{

int T;
scanf("%d",&T);
while (T--) {
scanf("%d",&n);
flag = 1;
for (int i = 1; i <= n; i++) {
e[i].clear();
paired[i] = 0;
}
for (int i = 1; i < n; i++) {
scanf("%d %d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}//加入边
if (n & 1) {
flag = 0;//剪枝
}
dfs(1, -1);//树
if (flag) {
printf("Yes\n");
}else
printf("No\n");
}
return 0;
}

笔试

  • 这次的难度相比上次真的体现出我大移动的光彩(其实也发现了电工理论是要做题,但是软设理论却除了考试外从未做过题的缺憾.)
  1. 最后的选择题

    交换的是指针,i和j并没有变化

    然后这个不是传引用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #include <iostream>
    #include <vector>
    using namespace std;

    class A{
    public:
    int* m;
    };

    void swap(A & a,A & b)
    {

    A temp;
    temp = a;
    a = b;
    b = temp;
    }
    int main()
    {

    int x = 1,y = 2;
    A a,b;
    a.m = &x,b.m = &y;
    swap(a, b);
    cout << *a.m << " " << *b.m << endl;
    cout << x << " " << y << endl;
    return 0;
    }//希望考试时候不要弄反顺序了= =

  2. vector的erase

    erase一个迭代器的时候是删除临时副本,迭代器后移

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {


    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(2);
    vec.push_back(3);

    for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
    if (*it == 2) {
    vec.erase(it);
    it--;
    }
    }
    for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << endl;
    }
    return 0;
    }

  3. 异常类的析构顺序

    xcode的显示是++--!—(有毒,希望看到这里的小伙伴能run一下)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    #include <iostream>
    #include <vector>
    using namespace std;

    class B{
    public:
    B(){
    cout << "+";
    }
    ~B(){
    cout << "-";
    }
    };
    void func(){
    B kk;
    throw kk;
    }

    int main()
    {

    try {
    B k;
    func();
    } catch (...) {
    cout << "!" ;
    }

    return 0;
    }
  4. 最后题意莫名的:

    不要提前交卷!!!!!!否则真·眼瞎还是很可怕的!!!!!!!!

  5. 总结:

    这次考的并不是很难,virtual还有hint,主要是没有摸索出一套科学的学习方法,而是在用工程的思想去学习,这也无可厚非吧.

    另外考完后不要打开软设群,不要刷朋友圈,有毒:)

三金留下的足迹

  • 前面几篇文章中三金都写下了或多或少的一些文字,很是感激
  • 但因为整理缘故只能在renld.duoshuo.com看到.现在记录在下面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
因为https的原因有些资源换访问错误

建议吧所有连接包括http://...和https://...都改成//...

多说居然会吞掉带有代码的评论

此评论用户状态为:【一般用户】;ip地址:【58.67.136.214】;用户uid为:【6294806818413609730

619日 发表于 期末机考最后一题



Mixed Content: The page at 'https://renld.github.io/2016/06/13/STL-LEARN/#more' was loaded over HTTPS, but requested an insecure script 'http://v3.jiathis.com/code/jia.js?uid=1405949716054953'. This request has been blocked; the content must be served over HTTPS.

/2016/06/13/STL-LEARN/#more:1 Mixed Content: The page at 'https://renld.github.io/2016/06/13/STL-LEARN/#more' was loaded over HTTPS, but requested an insecure script 'http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js'. This request has been blocked; the content must be served over HTTPS.

main.js:1 Uncaught ReferenceError: require is not defined

/2016/06/13/STL-LEARN/#more:1 Mixed Content: The page at 'https://renld.github.io/2016/06/13/STL-LEARN/#more' was loaded over HTTPS, but requested an insecure script 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'. This request has been blocked; the content must be served over HTTPS.

出错了,github默认支持https,的把那些资源url全都改成自动识别协议吧

比如

http://a.com

或者

https://a.com

都改成

//a.com

619日 发表于 期末机考最后一题
文章目录
  1. 1. 期末考试三套模拟卷
    1. 1.0.1. 12计科
      1. 1.0.1.1. T2
      2. 1.0.1.2. T9
      3. 1.0.1.3. T14
      4. 1.0.1.4. Error Find
    2. 1.0.2. 09软工
      1. 1.0.2.1. T4虚继承
      2. 1.0.2.2. T7注意是每次插入起始位置
    3. 1.0.3. 10软工
      1. 1.0.3.1. T3 reference
      2. 1.0.3.2. T6
      3. 1.0.3.3. 电脑没电后的笔记
  • 2. 软件设计II模板
  • 3. 期末
    1. 3.1. 期末附加题
      1. 3.1.1. AC的方法
    2. 3.2. 笔试
  • 4. 三金留下的足迹