标签:des c style class blog code
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 7681 Accepted
Submission(s): 3724
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 using namespace std; 6 int arr[101][101]; 7 int sum[101][101]; 8 int main() 9 { 10 int nn,i,j,ans,temp; 11 // freopen("test.in","r",stdin); 12 while(scanf("%d",&nn)!=EOF) 13 { 14 ans=0; 15 memset(sum,0,sizeof(sum)); 16 for(i=1;i<=nn;i++) 17 { 18 for(j=1;j<=nn;j++) 19 { 20 scanf("%d",&arr[i][j]); 21 sum[i][j]+=arr[i][j]+sum[i][j-1]; //求出每一层逐步之和 22 } 23 } 24 for(i=2;i<=nn;i++) 25 { 26 for(j=1;j<=nn;j++) 27 { 28 sum[i][j]+=sum[i-1][j]; //在和的基础上,逐步求出最大和 29 } 30 } 31 ans=0; 32 for(int rr=1;rr<=nn;rr++) 33 { 34 for(int cc=1;cc<=nn;cc++) 35 { 36 for(i=rr;i<=nn;i++) 37 { 38 for(j=cc;j<=nn;j++) 39 { 40 temp=sum[i][j]-sum[i][cc-1]-sum[rr-1][j]+sum[rr-1][cc-1]; 41 if(ans<temp) ans=temp; 42 } 43 } 44 } 45 } 46 printf("%d\n",ans); 47 } 48 return 0; 49 }
方法二:
采用状态压缩的方式进行DP求解最大值......!
代码:
1 /*基于一串连续数字进行求解的扩展*/ 2 /*coder Gxjun 1081*/ 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 using namespace std; 8 const int nn=101; 9 int arr[nn][nn],sum[nn][nn]; 10 int main() 11 { 12 int n,i,j,maxc,res,ans; 13 // freopen("test.in","r",stdin); 14 while(scanf("%d",&n)!=EOF) 15 { 16 memset(sum,0,sizeof(sum)); 17 for(i=1;i<=n;i++) 18 for(j=1;j<=n;j++) 19 { 20 scanf("%d",&arr[i][j]); 21 sum[j][i]=sum[j][i-1]+arr[i][j]; 22 } 23 ans=0; 24 for(i=1;i<=n;i++){ 25 26 for(j=i;j<=n;j++) 27 { 28 res=maxc=0; 29 for(int k=1;k<=n;k++) 30 { 31 maxc+=sum[k][j]-sum[k][i-1]; 32 if(maxc<=0) maxc=0; 33 else 34 if(maxc>res) res=maxc; 35 } 36 if(ans<res) ans=res; 37 } 38 } 39 printf("%d\n",ans); 40 } 41 return 0; 42 }
hdu-------1081To The Max,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/gongxijun/p/3778702.html