小编Ati*_*tiq的帖子

给定一个数组,你必须找到最大可能两个相等的总和

给定一个数组,你必须找到最大可能两个相等的和,你可以排除元素.

1,2,3,4,6,给定数组,我们可以有最多两个相等的和6 + 2 = 4 + 3 + 1

即4,10,18,22,我们可以得到两个相等的和18 + 4 = 22

除了蛮力找到所有计算并检查两个可能的相等和之外,你的解决这个问题的方法是什么?

编辑1:数组元素的最大数量为N <= 50,每个元素最多可达1 <= K <= 1000

编辑2:这是我的解决方案https://ideone.com/cAbe4g,对于每种情况,给定的时间限制为5秒,需要花费太多时间.

编辑3: - 总元素总和不能大于1000.

algorithm

15
推荐指数
1
解决办法
1808
查看次数

如果我将push函数调用超过8次,为什么我的代码会崩溃?

//每当我增加8次以上的函数调用时间时,它会在一切正常之前崩溃.你的帮助是必需的.在下面的代码中,我使用动态数组创建了一个堆栈程序,记住不应该有任何堆栈溢出,通过使用realloc函数在堆栈填充时将值加倍.

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
typedef struct ArrayStack
{
int top;
int capacity;
int *arr;
}*stack;

stack Creation()
{
 stack S;
 S=(stack)malloc(sizeof(struct ArrayStack));
 if(!S)return NULL;
 S->top=-1;
 S->capacity=1;
 S->arr=(int*)malloc(S->capacity*sizeof(int));
 if(!S->arr)return NULL;
 return S;
}
int is_Full(stack S)
{
return S->top==S->capacity-1;
}
int is_Empty(stack S)
{
return S->top==-1;
}

void Doubling(stack S)
{
 S->capacity*=2;
 S->arr=realloc(S->arr,S->capacity);
}

void push(stack S,int data)
{
if(is_Full(S)) 
Doubling(S);

S->arr[++S->top]=data;
}

int pop(stack S)
{
if(is_Empty(S))
printf("\nStack underflow");
else
return S->arr[S->top--];
}

int main()
{
stack S;
int i=0,size=9; …
Run Code Online (Sandbox Code Playgroud)

c stack-overflow stack

1
推荐指数
1
解决办法
108
查看次数

标签 统计

algorithm ×1

c ×1

stack ×1

stack-overflow ×1