Clion中调试和运行模式之间的结果奇怪不同

时间:2017-04-23 23:50:10

标签: c++ debugging long-integer clion

当我解决动态编程问题时,我发现了一个奇怪的问题。

代码

// Problem 01
// A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.

#include <cstdio>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

// 1st method
// top-down dynamic programming
long steps(int n, vector<long>& d) {
  if (d[n] > 0L)
    return d[n];
  //
  if (n == 1)
    return 1L;
  else if (n == 2)    // 1,1  2
    return 2L;
  else if (n == 3)    // 1,1,1  1,2   2,1   3
    return 4L;
  //
  long s = steps(n-1, d) + steps(n-2, d) + steps(n-3, d);
  d[n] = s;
  return s;
}

long solve(int n) {
  vector<long> d;
  for (int i = 0; i < n; ++i) {
    d.push_back(-1L);
  }
  long r = steps(n, d);
  return r;
}

// 2nd method
// bottom-up dynamic programming
// and remove the redundant array
long steps2(int n) {
  if (n == 1)
    return 1L;
  else if (n == 2)    // 1,1  2
    return 2L;
  else if (n == 3)    // 1,1,1  1,2   2,1   3
    return 4L;
  //
  long s = 0L;
  long s1 = 4L;
  long s2 = 2L;
  long s3 = 1L;
  for (int i = 4; i < n; ++i) {
    s = s1 + s2 + s3;
    s3 = s2;
    s2 = s1;
    s1 = s;
  }
  s = s1 + s2 + s3;
  return s;
}


long solve2(int n) {
  return steps2(n);
}


////////////////////// Test ////////////////////////
class Test {

 public:

  Test() {
    basicTests();
  }

 private:
  int num_fail = 0;

  void basicTests() {
    printf("C++ version: %ld\n", __cplusplus);
    // customize your own tests here
    printf("%d, %ld\n", 20, solve(20));
    printf("%d, %ld\n", 20, solve2(20));
  }

};


////////////////////// Main ////////////////////////
int main() {
  Test t = Test();  // change the method you want to test here.
  return 0;
}

结果

当我运行代码时,两种方法都会给出相同的结果,看起来是正确的。

C++ version: 201103
20, 121415
20, 121415

但是当我使用调试模式, 有时 时,第一个结果似乎是错误的,就像这样。

C++ version: 201103
20, 4210418572061733749
20, 121415

有什么问题?

1 个答案:

答案 0 :(得分:1)

repository/component/identity中,您使用solve值填充向量d,索引0 ... n-1。在步骤中,您会立即检查n值为d[n]的{​​{1}}。

你正在使用未定义的值 - 得到垃圾结果