Algorithmic Toolbox week2 homework 总结

2017-12-25 22:00
  今天是星期一,来总结一下周日上午和今天上午耗费大约4~5h的第二周作业。主要讲述一下两大模块,分别是学习到的技能和遭遇到的

技能

  • 学习了程序的自动化测试的具体实现过程。以c++代码为例,代码具体如下:
#include     //1

void test_solution() {
    assert(fibonacci_fast(3) == 2);     //2.1
    assert(fibonacci_fast(10) == 55); //2.2
    for (int n=0; n<20; ++n) {           //3.1
        assert(fibonacci_naive(n) == fibonacci_fast(n)); //3.2
    }
}
  1. 我们要引入cassert头文件
  2. 采用枚举比较法,比较fast函数和结果。
  3. 采用迭代比较法,逐一比较结果。

有些时候,我们还需要引入随机数,具体代码如下:

#include 
#include 

void test_solution() {
  assert(gcd_fast(4, 2) == 2);
  assert(gcd_fast(6, 4) == 2);
  assert(gcd_fast(6, 12) == 6);

  int a = 0;
  int b = 0;

  for (int i=0; i<100; ++i) {
    a = rand()%20 + 2;
    b = rand()%50 + 3;
    assert(gcd_fast(a, b) == gcd_naive(a, b));
  }
  cout<<"OK"<

注:引入cstdlib头文件,从而使用rand函数。

  • 深切体会到分段函数在编程中的重要使用,尤其是当输入值很小的情况下,以fibonacci为例:
int fibonacci_fast(int n) {
    if (n < 2) {    //1.1
        return n;  //1.2
    }                  // 1.3

    vector numbers(n+1);
    numbers[0] = 0;
    numbers[1] = 1;
    for (int i=2; i<=n; ++i) {
        numbers[i] = numbers[i-1] + numbers[i-2];
    }

    return numbers[n];
}

其中当n<2(即n=1或者n=0)时,直接返回n。在n<2的情况下,就避免了进行后续复杂运算(O(n)),而通过O(1)的操作就得到了正确的结果。

后面有一个程序,以下面代码为例:

int fibonacci_sum_naive_part(int n) {
    if (n <= 1)
        return n;

    long long previous = 0;
    long long current  = 1;
    long long sum      = 1;

    for (int i = 0; i < n - 1; ++i) {
        long long tmp_previous = previous;
        previous = current;
        current = tmp_previous + current;
        sum += current;
    }

    return sum % 10;
}

//m=10
int fibonacci_sum_naive(long long n) {
    if (n <= 1)
        return n;

    vector vec;
    int period_vec = get_fibonacci_period(vec, n, 10);
    // cout<<"period_vec "<
  • 区间转换,对于[from, to]类的问题,可以转化为[0, to] - [0, from-1](注意:这里是from-1,而不是from+1,不要搞错了)
long long get_fibonacci_partial_sum_naive(long long from, long long to) {
    long long result = 0;

    if (from >= 1) {
        result = (fibonacci_sum_naive(to) - fibonacci_sum_naive(from-1)) % 10;
    }

    else {
        result = (fibonacci_sum_naive(to)) % 10;
    }


    if (result < 0) {
        result += 10;
    }

    return result;
}

遇到的坑(自己犯的错)

1.求fibonacci数,有两种方法,一种是动态规划法(第五章),一种是table法。对于后者而言,需要注意的是,要求第n个fibanacci数,需要申请长度为n+1的数组(vector in c++),而不是n
2.修改函数名以后,忘记把相应的都修改了,导致错误 (error: 'gcd_fast' was not declared in this scope, error: 'lcm_fast' was not declared in this)。
3.忘记对一些调试的cout代码进行注释了,导致了输出格式错误。(Failed case #1/22: Cannot check answer. Perhaps output format is wrong.
)
4.对long long数据直接进行循环操作(循环次数达到超多次)。例如,当n为4534532453345时。

int fibonacci_sum_naive_part(long long n) {
    if (n <= 1)
        return n;

    long long previous = 0;
    long long current  = 1;
    long long sum      = 1;

    for (long long i = 0; i < n - 1; ++i) {
        long long tmp_previous = previous;
        previous = current;
        current = tmp_previous + current;
        sum += current;
    }

    return sum % 10;
}

5.一定要做数据测试,最起码也得枚举比较几个数据!!!
关于程序的每个题的思路,下周一再来写。

你可能感兴趣的:(Algorithmic Toolbox week2 homework 总结)