sjtu1002.二哥种花生

Description

二哥在自己的后花园里种了一些花生,也快到了收获的时候了。这片花生地是一个长度为L、宽度为W的矩形,每个单位面积上花生产量都是独立的。他想知道,对于某个指定的区域大小,在这么大的矩形区域内,花生的产量最大会是多少。

Input Format

第1行有2个整数,长度L和宽度W。

第2行至第L+1行,每行有W个整数,分别表示对应的单位面积上的花生产量A( 0A<10 )。

第L+2行有2个整数,分别是指定的区域大小的长度a和宽度b。

Output Format

输出一个整数m,表示在指定大小的区域内,花生最大产量为m。

Sample Input

4 5
1 2 3 4 5
6 7 8 0 0
0 9 2 2 3
3 0 0 0 1
3 3

Sample Output

38



// sjtu1002.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

int main()
{

	int length, height,use_length,use_height;//height_all,, numb_of_apple,numb_get=0
	int length_flag,height_flag,numb;//
	int loop_length,loop_height;
	int *a,*data_copy;
	int max_data;

	scanf("%d %d", &height,&length );//b[i]=buf;	

	if((height>1000)||(height<0)||(length>1000)||(length<0)) return 0;

	numb=(sizeof(int))*(length*height);
	
	//cout<<length<<endl;
	//cout<<height<<endl;
	//cout<<numb;

	a=(int *)malloc(sizeof(int)*(length*height));

	//numb_flag=0;

	for(height_flag=0; height_flag<height; height_flag++)
		for(length_flag=0; length_flag<length; length_flag++)
			cin>>a[height_flag*length+length_flag];
	
	scanf("%d %d", &use_height, &use_length);

	if((use_length>length)||(height<use_height)) return 0;

	
	loop_length=length-use_length+1;
	loop_height=height-use_height+1;	
	data_copy=(int *)malloc(sizeof(int)*(loop_length*height));
	memset(data_copy, 0, sizeof(int)*(loop_length*height));
	if(data_copy == NULL)
		cout<<"null"<<endl;

	//cout<<loop_length<<endl;;
	//cout<<loop_height<<endl;
	//cout<<numb<<endl;
	
	height_flag=0;
	length_flag=0;
	
	//cout<<"in"<<endl;
	for(height_flag=0;height>height_flag;height_flag++)  //calculate height * loop_length data
		for(length_flag=0;loop_length>length_flag;length_flag++)
			for(numb=0;numb<use_length;numb++)
			{
				data_copy[height_flag*loop_length+length_flag]+=a[height_flag*length+length_flag+numb];
				//cout<<data_copy[height_flag*loop_length+length_flag]<<endl;		//store height*loop_length
			}


	//cout<<"out1"<<endl;
	//for(height_flag=0;height>height_flag;height_flag++)
		//for(length_flag=0;loop_length>length_flag;length_flag++)
			//cout<<data_copy[height_flag*loop_length+length_flag]<<endl;
	
	memset(a, 0, sizeof(int)*(loop_length*loop_height));
	//cout<<"in2"<<endl;
	for(length_flag=0;loop_length>length_flag;length_flag++)  //calculate loop_height * loop_length data
		for(height_flag=0;loop_height>height_flag;height_flag++)
			for(numb=0;numb<use_height;numb++)
			{
				a[height_flag*loop_length+length_flag]+=data_copy[height_flag*loop_length+length_flag+numb*loop_length];
				//cout<<a[height_flag*loop_length+length_flag]<<endl;		//store loop_height*loop_length
			}
	
	
	//cout<<"out2"<<endl;
	//for(height_flag=0;loop_height>height_flag;height_flag++)
		//for(length_flag=0;loop_length>length_flag;length_flag++)
			//cout<<a[height_flag*loop_length+length_flag]<<endl;
	
	max_data=0;
	for(numb=0;numb<loop_length*loop_height;numb++)
		if(a[numb]>max_data) max_data=a[numb];

	cout<<max_data;

		

	return 0;
}


调试了半天,出了几个简单的错误。一一修改,通过。

1、内存分配后必须初始化,赋初值。不然运算出错。(memset)

2、我写的程序和题目中行列没有对应,导致结果一直有问题。

最后说下方法,用的方法主要分三步:

1、计算出其实就是算出每个height*loop_length单元的值

2、计算出loop_height*loop_length个单元的值

3、最后再对个单元进行比较,求出最大值。

备注:loop_height、loop_length是就是你每行每列存在a,b的个数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值