我一直在尝试解决一个涉及组合学和动态编程的编程难题。在
click here to read the problem
好消息是我已经解决了这个问题,但是对于某些输入,程序会抛出错误的答案。在
程序接受两个数字作为输入。
让w = argv[1]
,h = argv[2]
我要用伪数学的方式来写,以便于表达
例如,下面的“公式”意味着我的程序接受两个参数:w和h,输出是x
T(w,h) = x
我将用T(w,h)
表示理论结果,用R(w, h)
表示程序的结果。我百分之百确信T(w, h)
永远是正确的答案。在
我们走吧:
T(10, 10) = 2
R(10, 10) = 2
T(11, 10) = 2
R(11, 10) = 288
**结果不同**
T(12, 10) = 4
R(12, 10) = 4
T(13, 10) = 4
R(13, 10) = 4
T(14, 10) = 66
R(14, 10) = 66
T(15, 10) = 290
R(15, 10) = 290
T(20, 10) = 9594
R(20, 10) = 98826
结果不同
T(25, 10) = 419854
R(25, 10) = 419854
T(30, 10) = 94082988
R(30, 10) = 94082988
这一点的结果总是不同的
T(36, 10) = 19730715436
R(36, 10) = 18446744071965430572
数据类型溢出?
T(37, 10) = 19730715436
R(37, 10) = 18446744071965430572
T(38, 10) = 73368404198
R(38, 10) = 393345822
这个结果比上一个小,应该比最后一个大。
T(39, 10) = 287780277370
R(39, 10) = 17468538
T(40, 10) = 287780277370
R(40, 10) = 17468538
T(41, 10) = 1095232487336
R(41, 10) = 15826856
T(42, 10) = 4013757692218
R(42, 10) = 18446744071672822074
又涨了。计算的时间太长了
我想这已经足够黑盒测试了,现在让我们看看算法和实际代码。在
第一个参数乘以2,除以3并转换为整数。在
例如
argv1=20
20*2=40
40/3=13整数除法
这个值被传递给给给我问题的函数。在
设iNormalizedWidth
为该值。在
如果iNormalizedWidth
是奇数,程序会一直给我一个错误的答案。
只给我以下数字的错误答案:
11,20,25,29,35-48。在
(48是我的程序将处理的最大值)。在
这是我写的函数:
typedef long long int int64;
#define BLOCK_A = 2;
#define BLOCK_B = 3;
/* main function, more macros, prototypes of other functions and
irrelevant information for the scope of my question */
vector< vector<int64> > iTileBricks(int iWidth) {
int iK, i, j, iMaxIterations, iEvenWidth, iOffset;
vector<int64> viBrickRange;
vector< vector<int64> > viBrickWall;
vector< vector<int64> > viResult;
iEvenWidth = iWidth % 2;
iK = (int)(iWidth/2); // The amount of all possible combinations that follow the pattern nCr(iN-i,2i)
iMaxIterations = iK/3 + 1; // By dividing all the possible combinations by 3, I am finding out how the maximum amount of iterations
for(i = 0; i < iMaxIterations; i++) {
iOffset = 2*i + iEvenWidth;
vector<bool> vAux(iK-i); // Creating a iK - i long vector. Test Case:
// Let dOriginalPanelWidth = 48
// iPanelWidth = 32
// iK = iPanelWidth/2 = 16
// iMaxIterations = iK/3 ~= 5
// iK - i = 16 - i Where 1 <= i <= 5
// For the first iteration of i the value of vAux will be:
if(iOffset <= iK-i) {
fill(vAux.begin() + iOffset, vAux.end(), true); // For the first iteration of i the value of vAux will be:
}
// vAux = [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
do { // In this block of code I'm generating all the possible layouts of bricks that can build
for(j = 0; j < iK-i; j++) { // a wall of 48" (or any value of width where 3 <= width <= 48 and (width % 1/2) == 0).
if(vAux[j] == false) {
viBrickRange.push_back(j); // Generating n-tuples with all possible combinations
}
}
viBrickWall.push_back(viBrickRange);
viBrickRange.clear();
} while(next_permutation(vAux.begin(), vAux.end()));
for(unsigned int a=0; a < viBrickWall.size(); a++) {
vector<int64> viTmp(iK-i);
fill(viTmp.begin(), viTmp.end(), BLOCK_A);
for(unsigned int b = 0; b < viBrickWall[a].size(); b++) {
viTmp[viBrickWall[a][b]] = BLOCK_B;
}
viResult.push_back(viTmp);
viTmp.clear();
}
viBrickWall.clear();
vAux.clear();
}
return viResult;
}
我找到了一个用Python编写的程序,后者的功能只不过是从Python函数到C++的端口。如果有帮助,请参考:
^{2}$这是一个相当大的函数(也是一个问题),但是我已经试着调试了一整天,但是我还没有找到一个解决方案。在
有更多的计算生成最终值,但这是导致其他函数失败的函数。在
我想知道任何理论,为什么在某些输入下,答案飞涨,然后又回到过去。在
当我将我的函数与python并排编写的函数进行比较时 对于某些输入,输出是相同的,而对于其他一些输入(如上所示),输出是不同的。在
任何帮助都将不胜感激。在
非常感谢!在
我已经解决了这个问题,答案很简单,而且是对条件的细微修改。在
基本上,我生成了一组额外的可能的组合,为了消除这个问题,我需要扩展我的初始条件。在
基本上,就在:
vector<bool> vAux(iK-i);
我需要将以下所有说明附在以下条件中:
if(iOffset <= iK-i)
这解决了我的问题。在
谢谢大家关注我的问题!在
相关问题 更多 >
编程相关推荐