码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1050 to the max

时间:2014-11-10 21:29:32      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   sp   for   div   问题   log   bs   

/*

这是一道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         else
17             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                 else
20                     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

标签:blog   io   os   sp   for   div   问题   log   bs   

原文地址:http://www.cnblogs.com/qscqesze/p/4088059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!