背景:参考两个物联网PLC得值,两个PLC再一分钟内持续并达到一个目标得时候值加1,参考这篇文章:https://blog.csdn.net/shine_journey/article/details/55254947
#include<iostream> #include<string> using namespace std; int main() { std::string str = "987600000000000000000789111111111111111666666666666"; int num = 1; char word; int len = str.length()-1; for(int i=0; i<len; i++) { if (str[i] == str[i+1]) { word = str[i]; ++num; } else { num = 1; } if(num >= 11) // 这里11表示,相同值的个数至少是11个 { ++i; while (i < len) { if (str[i] == str[i+1]) { ++i; ++num; } else { break; } } //这里的输出是:从0开始计数,连续值起始位置 cout << "start point is :" << i-num+1 << endl; // num 表示连续相同值有多少个 cout << "count is : " << num << endl; num = 1; } } return 0; }
代码存放在 github ,
地址:
https://github.com/Long-Yucheng/BaseCode/blob/master/Algorithm/find same data in array.cpp