父亲给三个儿子分田(水题,但比较麻烦)

本文探讨了一个关于田地如何按照特定吨数分配给三个儿子的问题,并提供了求解此类问题的算法实现。通过输入田地尺寸、每块田地的玉米吨数以及每个儿子应得的吨数,该算法能够计算出满足条件的分配方式数量。
摘要由CSDN通过智能技术生成

                                     做这道题花了很长时间,代码能力太菜,存数组时就出现问题了,下面三个FOR循环都怎样都控制不好。

Three sons inherited from their father a rectangular corn fiend divided into n × m squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing AB and C tons of corn.

The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square.

Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew A tons of corn, B on another one and C on the remaining one.

Input

The first line contains space-separated integers n and m — the sizes of the original (1 ≤ n, m ≤ 50, max(n, m) ≥ 3). Then the field's description follows: n lines, each containing m space-separated integers cij, (0 ≤ cij ≤ 100) — the number of tons of corn each square contains. The last line contains space-separated integers A, B, C (0 ≤ A, B, C ≤ 106).

Output

Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained A tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0.

Sample Input

Input
3 3
1 1 1
1 1 1
1 1 1
3 3 3
Output
2
Input
2 5
1 1 1 1 1
2 2 2 2 2
3 6 6
Output
3
Input
3 3
1 2 3
3 1 2
2 3 1
5 6 7
Output
0
#include<stdio.h>
#include<string.h>
using namespace std;
int a[100][100],b[1000020],c[1000020];
int v[5],a1,a2,a3;
int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        int k=0,s=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                scanf("%d",&a[i][j]);
        scanf("%d %d %d",&a1,&a2,&a3);
        for(int i=0; i<n; i++)
        {
            for(int j=0;j<m;j++)
            {b[k]+=a[i][j];//把每行的和存入数组。
            }
            k++;

        }
        for(int i=0;i<m;i++)
        {for(int j=0; j<n; j++)
        {
            c[s]+=a[j][i];//把每列的和存入数组
        }
        s++;
        }
        int z=0,sum=0;
        for(int i=0; i<n-2; i++)//固定一部分,遍历
        {
            sum+=b[i];
            int num=0;
            for(int j=i+1; j<n-1; j++)//固定第二部分,遍历
            {
                num+=b[j];
                int l=0;
             for(int x=j+1;x<n;x++)//固定第三部分,遍历
                {l+=b[x];}
                v[0]=a1;
                v[1]=a2;
                v[2]=a3;//保证分给三个儿子的田地不变。
                int o=0;
                for(int d=0; d<3; d++)//查询是否分到a1,a2,a3,完成o++,加到3,记录一次
                {
                    if(sum==v[d]&&v[d]!=-1)
                    {   o++;
                        v[d]=-1;

                    }
                    if(num==v[d]&&v[d]!=-1)
                    {o++;
                    v[d]=-1;
                    }
                    if(l==v[d]&&v[d]!=-1)
                    {
                        o++;
                        v[d]=-1;
                    }
                    if(o==3)
                    {
                        z++;
                    }
                }
            }
        }
     //下面为列的所有查询
         int sum1=0;
        for(int i=0; i<m-2; i++)
        {
            sum1+=c[i];
            int num1=0;
            for(int j=i+1; j<m-1; j++)
            {
                num1+=c[j];
                int l1=0;
                for(int x=j+1;x<m;x++)
                {
                    l1+=c[x];
                }
                v[0]=a1;
                v[1]=a2;
                v[2]=a3;
                int o=0;
                for(int d=0; d<3; d++)
                {
                    if(sum1==v[d]&&v[d]!=-1)
                    {
                        v[d]=-1;
                        o++;
                    }
                    if(num1==v[d]&&v[d]!=-1)
                    {
                        v[d]=-1;
                        o++;
                    }
                    if(l1==v[d]&&v[d]!=-1)
                    {
                        v[d]=-1;
                        o++;
                    }

                    if(o==3)
                {
                    z++;
                }
                }
            }
        }
        printf("%d\n",z);
    }
}


内容概要:该库专为研究生入学考试计算机组成原理科目设计,涵盖名校考研真、经典教材课后习、章节库和模拟试四大核心模块。名校考研真精选多所知名高校的计算机组成原理科目及计算机联考真,并提供详尽解析,帮助考生把握考研命趋势与难度。经典教材课后习包括白中英《计算机组成原理》(第5版)和唐朔飞《计算机组成原理》(第2版)的全部课后习解答,这两部教材被众多名校列为考研指定参考书目。章节库精选代表性考,注重基础知识与重难点内容,帮助考生全面掌握考试大纲要求的知识点。模拟试依据历年考研真规律和热门考点,精心编制两套全真模拟试,并附标准答案,帮助考生检验学习成果,评估应试能力。 适用人群:计划参加研究生入学考试并报考计算机组成原理科目的考生,尤其是需要系统复习和强化训练的学生。 使用场景及目标:①通过研读名校考研真,考生可以准确把握考研命趋势与难度,有效评估复习成效;②通过经典教材课后习的练习,考生可以巩固基础知识,掌握解技巧;③通过章节库的系统练习,考生可以全面掌握考试大纲要求的各个知识点,为备考打下坚实基础;④通过模拟试的测试,考生可以检验学习成果,评估应试能力,为正式考试做好充准备。 其他说明:该库不仅提供详细的目解析,还涵盖了计算机组成原理的各个方面,包括计算机系统概述、数据表示与运算、存储器层、指令系统、中央处理器、总线系统和输入输出系统等。考生在使用过程中应结合理论学习与实践操作,注重理解与应用,以提高应试能力和专业知识平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值