My coding way (5)

本文介绍了一种解决最大子矩阵问题的算法,包括输入输出规范、问题描述、解题思路及示例输入输出。

问题 D: 最大子矩阵

时间限制: 1 Sec  内存限制: 128 MB

题目描述

有一个包含正数和负数的二维数组。一个子矩阵是指在该二维数组里,任意相邻的下标是1 x 1 或更大的子数组。一个子矩阵的和是指该子矩阵中所有元素的和。本题中,把具有最大和的子矩阵称为最大子矩阵。

例如,如下数组的最大子矩阵位于左下角,

 0 -2-7  0

 9  2-6  2

-4  1-4  1

-1  8 0 -2

最大子矩阵为

 9  2

-4  1

-1  8

其和为15

输入

第一行是一个测试数据的个数T,表示将会有T组测试数据。下一行输入一个正整数N,表示二维方阵的大小。接下来是N2个整数(由空格和换行隔开)。该数组的N2个整数,是以行序给出的。也就是,先是第一行的数,由左到右;然后是第二行的数,由左到右,等等。N可能达到100,数组元素的范围是[-127,127]

输出

与测试数据对应,刚好有T行,每行输出最大子矩阵的和。

样例输入

2

4

0 -2 -7 0

9 2 -6 2

-4 1 -4 1

-1 8 0 -2

3

3 9 -5

-9 4 -2

3 6 -7

样例输出

15

19

 

其实一开始让我做这道题我是拒绝的= = 因为第一次看这道题真的是浑身发抖ww 后来发现这道题和之前的那个矩阵思路差不多w

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int map[110][110];
int maxofmap;
int findinmap(int l,int h,int max)
{
    int i,j,p,q;
    int state=-1;
    int maxans;
    int tmp;
    for(i=0;i<max-l+1;i++)
    {
        for(j=0;j<max-h+1;j++)
        {
            tmp=0;
            for(p=0;p<l;p++)
            {
                for(q=0;q<h;q++)
                {
                    tmp+=map[1+p+i][1+q+j];
                }
            }
            if(state<0)
            {
                state++;
                maxans=tmp;
            }
            else if(tmp>maxans)
            {
                maxans=tmp;
            }
        }
    }
    return maxans;
}
void deal(int max)
{
    int l,h,tmp;
    for(l=1;l<=max;l++)
    {
        for(h=1;h<max;h++)
        {
            if((tmp=findinmap(l,h,max))>maxofmap)
            {
                maxofmap=tmp;
            }
        }
    }
    printf("%d\n",maxofmap);
}
 
 
int main()
{
    int all,n,i,j;
    scanf("%d",&all);
    for(;all>0;all--)
    {
        scanf("%d",&n);
        memset(map,0,sizeof(int)*110*110);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        maxofmap=map[1][1];
        deal(n);
    }
    return 0;
}


Currently You are now a website builder. These are the code in my cart.php & functions.php. Currently, i am very satisfied with my layout. However, I want to achieve cart like Shopee / Taobao / Lazada, that allow my customer to 1) Do selective checkout, where only selected items are sent to checkout page, and after order created, only these items will be removed from cart. On the other side, non-selected items are retained inside cart, until customer remove them manually through 清除購物車 / 移除選中商品 or customer check out them. Only remove when order is created. If customer go back from checkout page or payment page, or anything, as long as order is not created, dont remove them. Currently im using restore cart way to achieve this, but everything from cart is removed currently after order created. theres some mistake that stop me from achieving this. I turned on feature where customer can see how many items are inside cart through the cart icon at header. At the same time, i dont want my customer to notice that their cart is cleared and recovered before (the amount of items in cart icon will show 0 when havent recover and the number of items after recover). I dont want them to notice the change, therefore, we have to recover it fast until customer did not notice them. Please be considerate regarding customer that use PC because they might access my website through another tab, at there, they can see the amount of items in cart icon. Therefore, i want it to always show the correct amount, only remove when the order is created and maintain if the order is not created. I dont want them to notice how it works (recovering) 2) All these 全选 刪除選中的商品 清空購物車 输入优惠券代码 应用优惠券 已选商品: 0 件,共计: RM0.00 结算 works perfectly fine, please remain them. Maintaining all the layout and achieved functions. Please check which part stopped me from achieving the selective checkout (maintain non-selected items inside cart, only remove selected to checkout items when order created, and dont let customer nmotice the change in their cart. I want them to no matter access from where, either new tab, other device etc, they will see the correct items in their cart) For now, If i cancel the payment halfway, i am logged out when back from payment page, and when i check from other tab, non-selected items are removed from cart, only selected items are still inside cart. Also, when i login back after payment page, it redirects me to pay-only page. Please refer how taobao cart works, and review which part is stopping me from achieve taobao cart, give me the part of code to be changed and the new code to replaced with the current one, as i have no coding basic
最新发布
08-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值