首页 > 编程语言 >北大ACM poj1050 To the Max(C++)

北大ACM poj1050 To the Max(C++)

时间:2023-08-21 17:05:43浏览次数:35  
标签:integers poj1050 sub int Max sum C++ array rectangle


To the Max


Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 32446

 

Accepted: 16930


Description


Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:

9 2
-4 1
-1 8
and has a sum of 15.


Input


The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].


Output


Output the sum of the maximal sub-rectangle.


Sample Input


4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2


Sample Output


15


#include<stdio.h>
int s[100],n;
int zxl()//计算子矩阵最大子序列的和 
{
         int sum=0;
         int max=-100000;
         for(int i=0;i<n;i++)
         {
             sum+=s[i];
             if(sum>max)
                max=sum;
             if(sum<0)
		        sum=0;
         }
         return max;
}

int main()
{
    int a[110][110],max1;
    while(scanf("%d",&n)!=EOF)
    {
    	max1=-99999;
        for(int i=0;i<n;i++)
           for(int j=0;j<n;j++)
             scanf("%d",&a[i][j]);//输入矩阵 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                 s[j]=a[i][j];
            max1=zxl()>max1?zxl():max1;
            for(int j=i+1;j<n;j++)
            {
                for(int k=0;k<n;k++)
                 s[k]+=a[j][k];
                 max1=zxl()>max1?zxl():max1;
            }
        }
          printf("%d\n",max1);
    }
    return 0;
}


标签:integers,poj1050,sub,int,Max,sum,C++,array,rectangle
From: https://blog.51cto.com/u_10101161/7177249

相关文章

  • 8、C++ 继承
    参考资料:C++继承|菜鸟教程(runoob.com)面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行效率的效果。当创建一个类时,您不需要重新编写新的数据成员和成员函......
  • C++文档入口汇总
    一、C++参考手册(中文)cppreferencehttps://zh.cppreference.com/二、菜鸟教程(中文)https://www.runoob.com/cplusplus/cpp-tutorial.html三、C++的常用库及其文档:标准模板库(STL)提供了许多数据结构和算法,如向量、链表、队列、堆栈、映射和排序算法等。其中文文档链接:https://......
  • c++算法之哈希表
    啥是哈希表 哈希表,类似散列表,是一种存储数据的一种方式。只能说是有点奇葩。他是通过将值转换成数组的下标,也就是f[x]=x的意思,大家估计都能理解吧......
  • 4.5 C++ Boost 文件目录操作库
    Boost库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量、可移植、高效的C应用程序。Boost库可以作为标准C库的后备,通常被称为准标准库,是C标准化进程的重要开发引擎之一。使用Boost库可以加速C应用程序的开发过程,提高代码质......
  • 4.7 C++ Boost 多线程并发库
    Boost库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量、可移植、高效的C应用程序。Boost库可以作为标准C库的后备,通常被称为准标准库,是C标准化进程的重要开发引擎之一。使用Boost库可以加速C应用程序的开发过程,提高代码质......
  • 《介绍篇》c、c++和c#
    参考链接:https://www.zhihu.com/question/44483143/answer/1943311640?utm_source=zhihu&utm_medium=social&utm_oi=757371723308879872回答1C++是面向对象版本的CC#跟着两者都没有密切的关系,从语法上来说,是微软抄了一个Java并做了一些改进,来挽救VisualBasic的未尽事业。一开始......
  • C++友元函数和友元类的使用
    1.友元介绍在C++中,友元(friend)是一种机制,允许某个类或函数访问其他类的私有成员。通过友元,可以授予其他类或函数对该类的私有成员的访问权限。友元关系在一些特定的情况下很有用,例如在类之间共享数据或实现特定的功能。友元可以分为两种类型:类友元和函数友元。2.类友元类友元(Friend......
  • C++友元函数和友元类的使用
    1.友元介绍在C++中,友元(friend)是一种机制,允许某个类或函数访问其他类的私有成员。通过友元,可以授予其他类或函数对该类的私有成员的访问权限。友元关系在一些特定的情况下很有用,例如在类之间共享数据或实现特定的功能。友元可以分为两种类型:类友元和函数友元。2.类友元类友元(Friend......
  • C++友元函数和友元类的使用
    1.友元介绍在C++中,友元(friend)是一种机制,允许某个类或函数访问其他类的私有成员。通过友元,可以授予其他类或函数对该类的私有成员的访问权限。友元关系在一些特定的情况下很有用,例如在类之间共享数据或实现特定的功能。友元可以分为两种类型:类友元和函数友元。2.类友元类友元(Friend......
  • C++友元函数和友元类的使用
    1.友元介绍在C++中,友元(friend)是一种机制,允许某个类或函数访问其他类的私有成员。通过友元,可以授予其他类或函数对该类的私有成员的访问权限。友元关系在一些特定的情况下很有用,例如在类之间共享数据或实现特定的功能。友元可以分为两种类型:类友元和函数友元。2.类友元类友元(Friend......