标题:内存超限该怎么办?
只看楼主
云允赟
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-9-27
结帖率:100%
已结贴  问题点数:20 回复次数:6 
内存超限该怎么办?

程序代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,j,k,a=0,sum=0,min,max;
    int data[10000];
    int **s;
    scanf("%d",&k);
    s=(int **)malloc(sizeof(int*)*k); 
    for(i=0;i<k;i++)
    s[i]=(int *)malloc(sizeof(int)*k);
    for(i=0;i<k;i++)
        for(j=0;j<k;j++)
        s[i][j]=0;
    for(i=0;i<k;i++)
    {
        scanf("%d",&data[i]);
        s[i][0]=data[i];
    }
    for(i=0;i<k;i++)
        if(data[i]>=0)a++;
    if(a==0)
    {
        sum=0;
        min=data[0];
        max=data[k-1];
    }    
    else 
    {
        for(i=0;i<k;i++)
            for(j=1;j<k-i;j++)
            s[i][j]=s[i][j-1]+data[i+j];
        for(i=0;i<k;i++)
            for(j=0;j<k-i;j++)
            if(sum<s[i][j])
            {
                sum=s[i][j];
                min=data[i];
                max=data[i+j];
            }    
    }  
    printf("%d %d %d\n",sum,min,max);
    for(i=0;i<k;i++)
    free(s[i]);
    free(s);
    return 0;
}

搜索更多相关主题的帖子: int sum data for i++ 
2017-09-29 10:47
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:2 
报错在哪步标出来吧
2017-09-29 11:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:9 
这么喜欢贴图,故意让别人看得难受?
要么帖链接
https://www.
要么贴文字
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

这道题,开辟数组的就应该给0分。只需要遍历一次就行了,那为什么还要开辟数组保存不再使用的数组呢?
话说回来,这道题也是烂题
In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case).
对于 { 0, 0, 1, 0, 0 } 的意思是输出 i=0, j=2 吗?

If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
为什么要做一个跟原本要求不相干的行为?
而且,对于 { -1, 0, -1 } 该输出 0 -1 -1 还是 0 1 1 ?



2017-09-29 12:28
云允赟
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-9-27
得分:0 
回复 2楼 yangfrancis
能运行,但是提交的时候就是显示内存超限
2017-09-29 12:46
云允赟
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-9-27
得分:0 
回复 3楼 rjsp
抱歉,下次注意。
如果不开辟数组,那么该怎么保存首位及末尾的数据呢?直接在得到最大值的时候得到么?
2017-09-29 12:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:9 
按照题目要求,下列4种情况,分别应该输出什么?

5
0 0 1 0 0

5
-1 0 0 0 -1

5
-1 +1 0 -2 +2

5
0 -1 -2 -3 0
2017-09-29 14:38
云允赟
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2017-9-27
得分:0 
回复 6楼 rjsp
1 1 1
0 0 0
2 2 2
0 0 0

额······好像有点不对
2017-09-29 22:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481028-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016479 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved