编程之美2.16(包含2.15)

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<time.h>
#include<assert.h>
#include<limits.h>
#include<math.h>
#include<fstream>
#include<algorithm>
#include<float.h>
#include<vector>
using namespace std;
time_t t1,t2;
void Tic()
{
  t1=time(NULL);
 // for(int i=0;i<INT_MAX;++i);
}
void Toc()
{
   t2=time(NULL);
    std::cout<<"time:"<<(t2-t1)/3600<<"时:"<<(t2-t1)%3600/60<<"分:"<<(t2-t1)%60<<"秒"<<std::endl;
}
int RandInt(int low,int high)
{
 float temp=rand()/(static_cast<float>(RAND_MAX));
 int k=low+static_cast<int>((high-low)*temp);
 return k;
}
void PrintVector(const vector<int>&iV)
{
 for(vector<int>::const_iterator ite=iV.begin();ite!=iV.end();++ite)
 {
  std::cout<<*ite<<"  ";
 }
 std::cout<<std::endl;
}
int FindMaxSubSum(std::vector<int>&L)
{
 //求最大的子数组之和
 //L[0] 无效
 int maxSum=L[1];
 int *sum=new int[L.size()];
 L[0]=0;
 for(int i=0;i<L.size();++i)
  sum[i]=L[i];
 for(int i=1;i<L.size();++i)
 {
  if(L[i]<L[i]+sum[i-1])
   sum[i]=L[i]+sum[i-1];
  else
   sum[i]=L[i];
  if(sum[i]>maxSum)
   maxSum=sum[i];
 }
 return maxSum;
}
int FindMaxSubSum2(int **&P,int N,int M)
{//二维数组之和最大值
 int maxSum=0;
 int *tempL=new int[M+1];

 for(int i=0;i<N;++i)
 {
  for(int ii=i;ii<N;++ii)//i---ii
  {
             for(int col=0;col<M;++col)
    {
     tempL[col+1]=0;
     for(int row=i;row<=ii;++row)
     tempL[col+1]+=P[row][col];
    }
    vector<int>L(tempL,tempL+M+1);
   
   
    int max=FindMaxSubSum(L);
    std::cout<<"row:"<<i<<" row:"<<ii<<" maxSum:"<<max<<std::endl;
    if(max>maxSum)maxSum=max;
  }
 }

 return maxSum;
}
void RunAlgorithm()
{
  /*3
  6
  1 -2 3 5 -3 2
  6
  0 -2 3 5 -1 2
  6
  -9 2 -3 -5 -3*/
 //std::ifstream in;
 //in.open("in.txt",std::ios::in);
 //int k=0;
 //in>>k;
 //std::vector<int>L;
 //   while(k)
 //{
 // L.clear();
 // L.push_back(0);//填充一个哨岗
 // int temp;
 // int n=0;
 // in>>n;
 // while(n)
 // {
 //  in>>temp;
 //  L.push_back(temp);
 //  n--;
 // }
 // std::cout<<FindMaxSubSum(L)<<std::endl;
 // k--;

 //}
 //in.close();

 /* 1
 3 3
 1 2 3
 4 5 6
 7 8 -100*/
 std::ifstream in;
 in.open("in.txt",std::ios::in);
    int n=0,N=0,M=0;
 in>>n;
 while(n)
 {  
  N=M=0;
  in>>N>>M;
  int **P=new int*[N];
  for(int i=0;i<N;++i)
  {
   P[i]=new int[M];
   for(int j=0;j<M;++j)
    in>>P[i][j];
  }
  std::cout<<FindMaxSubSum2(P,N,M)<<std::endl;
  for(int i=0;i<N;++i)
   delete []P[i];
  delete []P;
  n--;
 
 }

 

 in.close();
 
}

int _tmain(int argc, _TCHAR* argv[])
{
 
    RunAlgorithm();
   
 system("pause");

 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值