数组里大于特定值的序列的算法,还要考虑其容错性
求一个数组里大于特定值的序列的算法,还要考虑其容错性。
如题:::
我现在有这样一组数组,里面的值是这样的 17 4 11 9 8 20 5 1 2 12 15 2 3 2 5 18 11 12 6 19 1 4 20
我要把数组里的两类数据的个数提取出来:
1)连续的大于等于5的数,在这例子中应该是11 9 8 20 5。但是我们观察到前面有一个17,4这两个数,中间夹着4这个数,虽然比5小,但是如果比5小的个数不超过3个,我们还是认为它跟后面一大串大于等于5是连续的。因此:符合条件的序列应该是17 4 11 9 8 20 5 1 2 12 15 共11个数。此情况的序列最小长度是5。
2)连续的大于等于10的数,在这例子中应该是18 11 12,同理,看到后面的19前面有一个6虽然比10小,如果比10小的个数不超过2个,我们认为跟前面 18 11 12是连续的。因此:符合条件的序列应该是18 11 12 6 19。此情况的序列最小长度是3.
如果两序列的情况混合着的,则认为都是符合条件的,是一个或的关系。
在以下的C++代码基础上怎么修改:
#include<iostream>
#include<vector>
using namespace std;
#define COMPARE 5
bool is_Error(vector<int> num, int position){ //为误差
if (position == 0 || position == num.size() - 1)
return false;
if (num[position - 1] < COMPARE&&num[position + 1] < COMPARE)
return false;
if (position>1 && num[position - 2] < COMPARE&&num[position - 1] < COMPARE)
return false;
if (position < num.size() - 2 && num[position + 1] < COMPARE&&num[position + 2] < COMPARE)
return false;
return true;
}
void Get_Longest(vector<int> num, int* start, int *length){ //
int curr_length = 0;
bool search = true;
for (int i = *start; i < num.size(); ++i){
if (num[i] < COMPARE && search)
continue;
if (num[i] < COMPARE && !is_Error(num, i)){
*length = curr_length;
*start = i - curr_length;
return;
}
else{
search = false;
++curr_length;
}
}
++*start;
*length = curr_length;
}
void Print(vector<int> num, int start, int length){ //打印
if (!length)
return;
for (int i = 0; i < length; ++i)
cout << num[start + i] << ' ';
cout << endl;
}
int main(){
vector<int> num = { 1, 3, 2, 9, 4, 6, 9, 8, 9, 7, 5, 6, 9, 1, 4, 2, 5, 1, 6, 2 };
int start = 0, length = 0;
while (start < num.size()){
Get_Longest(num, &start, &length);
//if (length>3)
Print(num, start, length);
start += length;
}
}
------解决方案--------------------
1.Min_Length是这一段数当中没有误差的序列长度,就你所说的最小长度。
2.哦,你想把多个长度也输出来啊!如果你想直接跟在序列之后输出本序列长度,就在Print函数里最后加上相应的输出length即可。如果你想把多个序列的长度存起来再输出,可以在全局放一个vector数组,在同样的位置把length给push_back进去。然后在Get_Print_Arr最后把这个vector数组输出来。当然,如果程序很大的话全局变量不推荐使用,那就要使用参数来传递。
示例:
如题:::
我现在有这样一组数组,里面的值是这样的 17 4 11 9 8 20 5 1 2 12 15 2 3 2 5 18 11 12 6 19 1 4 20
我要把数组里的两类数据的个数提取出来:
1)连续的大于等于5的数,在这例子中应该是11 9 8 20 5。但是我们观察到前面有一个17,4这两个数,中间夹着4这个数,虽然比5小,但是如果比5小的个数不超过3个,我们还是认为它跟后面一大串大于等于5是连续的。因此:符合条件的序列应该是17 4 11 9 8 20 5 1 2 12 15 共11个数。此情况的序列最小长度是5。
2)连续的大于等于10的数,在这例子中应该是18 11 12,同理,看到后面的19前面有一个6虽然比10小,如果比10小的个数不超过2个,我们认为跟前面 18 11 12是连续的。因此:符合条件的序列应该是18 11 12 6 19。此情况的序列最小长度是3.
如果两序列的情况混合着的,则认为都是符合条件的,是一个或的关系。
在以下的C++代码基础上怎么修改:
#include<iostream>
#include<vector>
using namespace std;
#define COMPARE 5
bool is_Error(vector<int> num, int position){ //为误差
if (position == 0 || position == num.size() - 1)
return false;
if (num[position - 1] < COMPARE&&num[position + 1] < COMPARE)
return false;
if (position>1 && num[position - 2] < COMPARE&&num[position - 1] < COMPARE)
return false;
if (position < num.size() - 2 && num[position + 1] < COMPARE&&num[position + 2] < COMPARE)
return false;
return true;
}
void Get_Longest(vector<int> num, int* start, int *length){ //
int curr_length = 0;
bool search = true;
for (int i = *start; i < num.size(); ++i){
if (num[i] < COMPARE && search)
continue;
if (num[i] < COMPARE && !is_Error(num, i)){
*length = curr_length;
*start = i - curr_length;
return;
}
else{
search = false;
++curr_length;
}
}
++*start;
*length = curr_length;
}
void Print(vector<int> num, int start, int length){ //打印
if (!length)
return;
for (int i = 0; i < length; ++i)
cout << num[start + i] << ' ';
cout << endl;
}
int main(){
vector<int> num = { 1, 3, 2, 9, 4, 6, 9, 8, 9, 7, 5, 6, 9, 1, 4, 2, 5, 1, 6, 2 };
int start = 0, length = 0;
while (start < num.size()){
Get_Longest(num, &start, &length);
//if (length>3)
Print(num, start, length);
start += length;
}
}
------解决方案--------------------
1.Min_Length是这一段数当中没有误差的序列长度,就你所说的最小长度。
2.哦,你想把多个长度也输出来啊!如果你想直接跟在序列之后输出本序列长度,就在Print函数里最后加上相应的输出length即可。如果你想把多个序列的长度存起来再输出,可以在全局放一个vector数组,在同样的位置把length给push_back进去。然后在Get_Print_Arr最后把这个vector数组输出来。当然,如果程序很大的话全局变量不推荐使用,那就要使用参数来传递。
示例:
#include<iostream>
#include<vector>
using namespace std;
int COMPARE = 5; //The number compared
int INTERVAL = 2; //The maxinum of errors allowed
vector<int> arr_length;
int Get_Arr(vector<int> num, int* start, int *length){ //Get numbers