不完美版本一
#include<iostream> using namespace std; /*int yiwei_max(int n,int a[],int *p,int *q) { int temp=0,sum=-999999999,timer=-1; for(int i=0;i<n;i++) { if(temp>0) { temp+=a[i]; } else { temp=a[i]; } if(temp>sum) { sum=temp; *q=i; timer++; } } *p=*q-timer; return sum; }*/ int max_sum(int n,int a[],int *besti,int *bestj) { int *b = (int *)malloc(n * sizeof(int)); int sum = 0; int i = -1; int temp = 0; for (i=0;i<=n-1;i++) { if (temp > 0) { temp += a[i]; } else { temp = a[i]; } b[i] = temp; } sum = b[0]; for (i=1;i<=n-1;i++) { if (sum < b[i]) { sum = b[i]; *bestj = i; } } for (i = *bestj;i >= 0;i--) { if (b[i] == a[i]) { *besti = i; break; } } free(b); return sum; } void main() { int a[100][100],b[100]; int up[100],down[100],t[100]; int i,j,m,n,x,y; int temp,t2; int l=0,u=0,l_down,l_up,n_down,n_up; int s; cout<<"几行几列?"<<endl; cin>>m>>n; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[j]=a[i][j]; } //temp=yiwei_max(n,b,&x,&y); temp=max_sum(n,b,&x,&y); up[i]=x; down[i]=y; t[i]=temp; } t2=t[0]; for(i=0;i+1<m;i++) { if(up[i]<=down[i+1] && down[i]>=up[i+1]) { t2+=t[i+1]; } else { l_down=down[i]; l_up=up[i]; n_up=up[i+1]; n_down=down[i+1]; if(down[i]<up[i+1]) { for(;l_down!=up[i+1];) { l+=a[i][++l_down]; } for(;n_up!=down[i];) { u+=a[i+1][--n_up]; } } if(up[i]>down[i+1]) { for(;l_up!=down[i+1];) { l+=a[i][--l_up]; } for(;n_down!=up[i];) { u+=a[i+1][++n_down]; } } s=l>u?l:u; if(s+t[i+1]>0) { t2+=t[i+1]+s; } } } cout<<t2<<endl; }
时间: 2024-11-11 20:24:18