核心方法暴力搜素
剪枝的关键在于:
1利用空间复杂度换取时间复杂度,查找某个元素是否在数列中时,直接利用总长的数组,该数组已在第一次赋值时进行标记。
2由等差数列的长度要求,减少公差的枚举次数,枚举公差后对应的枚举首项时,去掉公差对应的不可能的首项。
upperdef = upper/(n-1); //第一次剪枝
for ( def = 1; def<=upperdef; def++) // array判定数组
{
for ( p = 0; places[p]<=(upper-((n-2)*def)); p++) {//第二次剪枝
bool is;
is = true;
int where;
for (int c = (n-1); c>=0 ; c--)
if (!array[places[p]+c*def]) {
is = false;
where = (p+c*def);
break;
}
if (is) {
noneF = false;
out<<places[p]<<" "<<def<<endl;
}
}
}
if (noneF)
out<<"NONE"<<endl;
return 0;
}