题解 | #最大连续子序列#
最大连续子序列
https://www.nowcoder.com/practice/afe7c043f0644f60af98a0fba61af8e7
#include <iostream> #include <cstring> #include <iostream> #include<climits> using namespace std; const int maxn = 10001; const int inf = INT_MAX; int arr[maxn]; int memo[maxn]; int start[maxn]; void fib(int n) { int maxm = -inf; for (int i = 0; i < n; i++) { if (i == 0) memo[i] = arr[i]; else { // memo[i] = max(arr[i], arr[i] + memo[i - 1]); if(arr[i]<arr[i]+memo[i-1]){ start[i]=start[i-1]; memo[i]=arr[i]+memo[i-1]; } else{ memo[i]=arr[i]; } } } } int main() { int n; while (cin >> n) { // 注意 while 处理多个 case if(n==0) break; int flag=0; memset(memo,0,sizeof(memo)); for(int i=0;i<n;i++){ cin>>arr[i]; if(arr[i]>=0) flag=1; start[i]=arr[i];//起始是自己 } if(flag==0) cout<<0<<" "<<arr[0]<<" "<<arr[n-1]<<endl; else{ fib(n); int maxIndex=0; int max=memo[0]; for(int i=0;i<n;i++){ if(max<memo[i]){ max=memo[i]; maxIndex=i; } } cout<<max<<" "<<start[maxIndex]<<" "<<arr[maxIndex]<<endl; } } } // 64 位输出请用 printf("%lld")