-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
std::bad_alloc 힙메모리 부족 문제
20.12.22 22:58 작성 조회수 1.57k
0
int main(){
freopen("input.txt", "rt", stdin);
int n, k, sum = 0, max = 0;
std::vector<int> temp(n); //Create int type vector with the size of 'n', initialize it as 0
scanf("%d %d", &n, &k);
for(int i=0; i<n; ++i){
//Storing input values in the integer array
scanf("%d", &temp[i]);
}
//Increment the sum of first mth numbers in a vector
for(int i=0; i<k; ++i){
sum += temp[i];
}
max = sum;
//Then from mth number use special formula to calculate the sum of adjacent numbers
for(int i=k; i<n; ++i){
sum = sum + (temp[i] - temp[i-k]);
if(sum > max) max = sum;
}
printf("%d", max);
return 0;
}
선생님이 주신 소스코드랑 변수명만 다르고 똑같이 한거같은데
이걸로 돌리면 std::bad_alloc 힙메모리가 터졌다는 에러가
뜨면서 실행이 안되더라구요...
정답코드는 잘 돌아갑니다! 혹시 제 코드랑 선생님 소스코드랑
다른점이 무엇인지 알 수 있을까요?
답변을 작성해보세요.
1
답변 1