返回一个整数数组中最大子数组的和

 

设计思路:

简单的动态规划问题,再加上文件读写和随机数生成两个知识点,即可写出

源程序代码:

#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
int main()
{


//测试数组 2 3 -1 -5 6 -1 4
  long long s[200];
  long long maxsum,i,j;
   srand(time(0));
 ofstream outfile;
      outfile.open("numfile.txt");
   for(i=0;i<100;++i){
      j=rand()%(10000-(-10000)+1)+(-10000);
      outfile<<j<<endl;
      }
      outfile.close();
      ifstream infile;
      infile.open("numfile.txt");
      i=0;
      while(infile){
         if(i==100)break;
         infile>>s[i++];
         cout<<"s["<<i-1<<"]:"<<s[i-1]<<endl;

         }
      maxsum=s[0];
  for(i=1;i<100;++i){
     s[i]=max(s[i]+s[i-1],s[i]);
     maxsum=max(maxsum,s[i]);
     }

  cout<<maxsum<<endl;


    return 0;
}
原文地址:https://www.cnblogs.com/deepend/p/12366235.html