首页 > 代码库 > POJ 1050 to the max

POJ 1050 to the max

/*

这是一道DP题

首先我们得明白一种题的做法

就是给n个数,让你输出其中一段连续字串和的最大值

*/

 1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6     int num[10000]; 7     int n; 8     cin>>n; 9     int temp=0;10     int max=-100000;11     for(int i=0;i<n;i++)12     {13         cin>>num[i];14         if(temp>0)15             temp+=num[i];16         else17             temp=num[i];18         if(max<temp)19             max=temp;20         cout<<max<<" ";21     }22     printf("\n");23     cout<<max<<endl;24     return 0;25 }

//以上是简单的数学问题,自己在本子上画画就知道是为啥了

 1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 int m[101][101]; 5 int main() 6 { 7     int n,max,i,j,tmp,k; 8     while(cin>>n) 9     {10         max=-10000;11         for(i=0;i<n;i++)12         {13             tmp=0;14             for(j=0;j<n;j++)15             {16                 cin>>m[i][j];17                 if(tmp>0)18                     tmp+=m[i][j];19                 else20                     tmp=m[i][j];21                 if(tmp<max)22                     max=tmp;23             }24         }//计算出哪一行中最大的连续字串和25         for(i=0;i<n;i++)26         {27             for(j=i+1;j<n;j++)28             {29                 tmp=0;30                 for(k=0;k<n;k++)31                 {32                     m[i][k]+=m[j][k];33                     if(tmp>0)tmp+=m[i][k];34                     else tmp=m[i][k];35                     if(tmp>max)max=tmp;36                 }37             }38         }39         cout<<max<<endl;40     }//类似一个前缀和的操作,然后直接类似于前文的操作直接暴力扫一遍即可41     return 0;42 }

 

POJ 1050 to the max