阵列调试不正确的输出,复杂算法
ARRAYS DEBUGGING incorrect outputs, complex algorithm
我制作了此算法,我正在调试它,以了解它为什么不起作用,但是随后我在每个周期结束时开始在打印阵列时开始遇到奇怪的东西,以查看问题首次发生的地方。乍一看,似乎我的周期没有考虑到最后一个数组的价值,但我不知道...有关算法和所有内容的所有信息都在源中。我想了解的主要是这个问题的答案:为什么输出有时会改变?如果我运行该程序,则有60-70%的时间我会得到答案14(这应该是错误的),但是有时候我会得到怪异的东西,因为结果……为什么?如果我不断获得不同的结果,我该如何调试代码....另外,如果我编译以进行发布而不是调试(在此处的Debian Sid中可用的最新海湾cod codeBlocks运行CodeBlocks),我将在大多数情况下获得9次。<<<<<。/p>
代码:
#include <iostream>
#include <vector>
/*void print_array
{
std::cout<<" ( ";
for (int i = 0; i < n; i++) { std::cout<<array[i]<<" "; }
std::cout<<")"<<std::endl;
}*/
///this algorithm must take an array of elements and return the maximum achievable sum
///within any of the sub-arrays (or sub-segments) of the array (the sum must be composed of adjacent numbers within the array)
///it will squeeze the array ...(...positive numbers...)(...negative numbers...)(...positive numbers...)...
///into ...(positive number)(negative number)(positive number)...
///then it will 'remove' any negative numbers in case it would be convienent so that the sum between 2 positive numbers
///separated by 1 negative number would result in the highest achievable number, like this:
// -- (3,-4,4) if u do 'remove' the negative number in order to unite the positive ones, i will get 3-4+4=3. So it would
// be better not to remove the negative number, and let 4 be the highest number achievable, without any sums
// -- (3,-1,4) in this case removing -1 will result in 3-1+4=6, 6 is bigger than both 3 and 4, so it would be convienent to remove the
// negative number and sum all of the three up into one number
///so what this step does is shrink the array furthermore if it is possible to 'remove' any negatives in a smart way
///i also make it reiterate for as long as there is no more shrinking available, because if you think about it not always
///can the pc know if, after a shrinking has occured, there are more shrinkings to be done
///then, lastly, it will calculate which of the positive numbers left is highest, and it will choose that as remaining maximum sum :)
///expected result for the array of input, s[], would be (i think), 7
int main() {
const int n=4;
int s[n+1]={3,-2,4,-4,6};
int k[n+1]={0};
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
int i=0, j=0;
// step 1: compress negative and postive subsegments of array s[] into single numbers within array k[]
/*while (i<=n)
{
while (s[i]>=0)
{
k[j]+=s[i]; ++i;
}
++j;
while (s[i]<0)
{
k[j]+=s[i]; ++i;
}
++j;
}*/
while (i<=n)
{
while (s[i]>=0)
{
if (i>n) break;
k[j]+=s[i]; ++i;
}
++j;
while (s[i]<0)
{
if (i>n) break;
k[j]+=s[i]; ++i;
}
++j;
}
std::cout<<"STEP 1 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
j=0;
// step 2: remove negative numbers when handy
std::cout<<"checked WRONG! "<<unsigned(k[3])<<std::endl;
int p=1;
while (p!=0)
{
p=0;
while (j<=n)
{
std::cout<<"checked right! "<<unsigned(k[j+1])<<std::endl;
if (k[j]<=0) { ++j; continue;}
if ( k[j]>unsigned(k[j+1]) && k[j+2]>unsigned(k[j+1]) )
{
std::cout<<"checked right!"<<std::endl;
k[j+2]=k[j]+k[j+1]+k[j+2];
k[j]=0; k[j+1]=0;
++p;
}
j+=2;
}
}
std::cout<<"STEP 2 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
j=0; i=0; //i will now use "i" and "p" variables for completely different purposes, as not to waste memory
// i will be final value that algorithm needed to find
// p will be a value to put within i if it is the biggest number found yet, it will keep changing as i go through the array....
// step 3: check which positive number is bigger: IT IS THE MAX ACHIEVABLE SUM!!
while (j<=n)
{
if(k[j]<=0) { ++j; continue; }
p=k[j]; if (p>i) { std::swap(p,i); }
j+=2;
}
std::cout<<std::endl<<"MAX ACHIEVABLE SUM WITHIN SUBSEGMENTS OF ARRAY : "<<i<<std::endl;
return 0;
}
可能会有问题,因为我不使用向量?感谢您的帮助!
编辑:我发现了两个算法错误!一个是用户M24P提到的一个,它在该算法的步骤1中找到,我用有点固定的旋转固定了,稍后再清理...另一个在步骤2中找到。看来,在当时的表达式检查中,我在其中检查一些数组的未符号值,真正检查的是,某些怪异数字的无符号值再次。我用简单的cout输出进行了测试:如果我做未签名(k [AnyIndexOfk]),并且该位置中包含的值是一个正数,我会得到未签名的正常数字但是,如果该数字为负,则该值不会简单地签名,而是看起来非常不同,就像我跨过数组之类的东西一样...我在IM期望-2返回为2或2或-4为4。(该数字适用于-4,-2给出4294967294)
我用新内容编辑了来源,感谢您的帮助!
编辑2: nvm我使用c 的cmath libs解决了std :: abs()如果不使用ABS,还有其他方法吗?
在您的代码中,您有:
while (s[i]>=0)
{
k[j]+=s[i]; ++i;
}
s初始化S so
int s[n+1]={3,-2,4,-4,6};
这是一个明显的错误。您的时循环会超越数组,并击中可能会或可能不会将其归零的垃圾数据。没有什么可以阻止我大于n 1。清理您的代码,以免超越阵列,然后尝试调试它。另外,您的问题是要使我感到舒适地回答您的问题需要更加具体,但是修复像我指出的那样的错误应该使停止遇到不一致,不确定的行为并开始专注于您的算法变得更加容易。我很想回答这个问题,但我只是无法解析您的特殊要求或出了什么问题。
- 如何为指向复杂值的迭代器专门化算法?
- 为什么一种算法在相同的时间复杂度下比另一种算法更快?
- 如何在复杂的算法中处理goto函数?
- 如何降低 c++ 中算法的时间复杂度?
- 这个算法的运行时间复杂度是多少?你是如何分析的
- 如何证明以下算法具有 O(nlogn) 时间复杂度
- 三和算法的时间复杂度是多少
- 关于算法的时间复杂度
- 从QTcpSocket上的数据流中连续运行复杂算法的最佳Qt线程解决方案是什么
- 迭代算法的时间复杂度
- 气泡排序算法的空间复杂度.
- 阵列调试不正确的输出,复杂算法
- 最短路径的dijkstra算法的时间复杂度是否取决于所使用的数据结构?
- 这个算法的时间复杂度是多少
- 下面代码的算法复杂度是多少
- malloc()/free()/new/delete/delete[] 的算法复杂度是多少?
- 俄罗斯农民乘法算法的时间复杂度.
- 从字符串中提取数字/数字范围的复杂算法
- 用Ruby on Rails实现复杂算法
- 如何从复杂算法中提取活动代码路径