感谢大佬的帮忙,就下面这道题啊啊啊啊
已知输入系列中各元素的值至少有两个元素,设计算法,求出该序列中元素的所有最大升降子序列。例如(1.20.30.12.3.5.7.4.6.100)输出为(1.20.30).(30.12.3).(3.5.7).(7.4).(4.6.100)
#include <iostream> #include <string> #include <vector> #include <stdlib.h> using namespace std; int main() { string str; cout << "请输入:"; cin >> str; int len = str.size(); // 1.20.30.12.3.5.7.4.6.100 vector<string> vec; int cursor = -1; while (cursor < len) { int index = cursor + 1; cursor = str.find('.', cursor + 1); if (cursor < 0) { vec.push_back(str.substr(index)); break; } vec.push_back(str.substr(index, cursor - index)); } if (vec.size() < 3) { cout << '(' << str << ")"; return 0; } int markIndex = -1; int count = 1; for (int i = 1; i < vec.size() - 1; i++) { int vec0 = atoi(vec[i-1].c_str()); int vec1 = atoi(vec[i].c_str()); int vec2 = atoi(vec[i+1].c_str()); int cmp_res = (vec0- vec1) * (vec1 - vec2); if (markIndex < 0) { if (cmp_res > 0) { markIndex = i-1; count += 2; } else { cout << '(' << vec[i-1] << '.' << vec[i] << ")."; markIndex = -1; count = 1; } } else if(cmp_res > 0) { count++; } else { cout << '('; for (int j = markIndex; j < markIndex + count; j++) { cout << vec[j]; if(j < markIndex + count - 1) { cout << '.'; } } cout << ")."; markIndex = -1; count = 1; } if (i == vec.size() -2) { if (markIndex < 0) { cout << '(' << vec[i] << '.' << vec[i+1] << ')'; } else { cout << '('; for (int j = markIndex; j < markIndex + count; j++) { cout << vec[j]; if(j < markIndex + count - 1) { cout << '.'; } } cout << ")"; } } } return 0; }
[此贴子已经被作者于2020-3-15 20:12编辑过]
[此贴子已经被作者于2020-3-16 10:11编辑过]
[此贴子已经被作者于2020-3-17 18:27编辑过]