資料結構學習筆記(1):Maximum Subsequence Sum最大子列和
阿新 • • 發佈:2019-01-28
問題
思路分析:
就是課堂上所講過的最大子列和問題,不過需要輸出子列頭和尾的項
根據網上的資料,摹寫程式碼為
具體實現:
#include<iostream> using namespace std; int main (){ int N ; cin >> N; int *p = new int[N]; for (int i = 0;i < N;i++) cin >> p[i]; int Maxsum = 0; int Thissum = 0; int start = 0, end = N - 1; int temp_start = 0; int temp_end = 0; for (int j = 0; j < N; j++) { if (Thissum >= 0) { Thissum += p[j]; temp_end = j; } else { Thissum = p[j]; temp_start = j; temp_end = j; } if ( Thissum > Maxsum || (Thissum ==0 )&&( end = N - 1)) { Maxsum = Thissum; start = temp_start; end = temp_end; } } cout << Maxsum<<' '<<p[start]<<' '<<p[end] << endl; system("PAUSE"); delete[] p; return 0; }