[백준 21919] 소수 최소 공배수 문제 - (Out Of Range)Runtime Error #6
-
4주차(에라토스테네스의 체) 과제 중 백준 21919번 문제에 오류가 생겨 질문 남깁니다! //https://www.acmicpc.net/problem/21919
#include <vector>
#include <iostream>
using namespace std;
vector<bool> PN(1, false);
void PrimeNum(int size) { //에라토스테네스의 체
//PN.resize(size+1); //원래 코드 (바로 아랫줄로 고친 것임!)
PN.resize(size+10); //out of range 오류 발생 시 배열의 크기를 늘려보라고 해서 늘려봤음. 여전히 런타임 에러가 뜬다..
for (int i = 2; i <= size; i++) {
if (PN.at(i) == true) continue;
for (int j = 2; i * j <= size; j++) {
PN[i * j] = true;
}
}
}
// PrimeNum 함수 : size를 입력받아, 그만큼 소수 배열의 크기를 정한다
//(bool 소수 배열과 사용자 입력을 비교해 소수인지 판별)
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N;
int count = 0;
cin >> N; //수열A의 길이(1 이상 10,000 이하)
vector<int> vec1(N);
int temp;
for (int i = 0; i < N; i++) {
cin >> temp;
vec1[i] = temp;
}
PrimeNum(vec1[N - 1]); //size 넘긴다 //순열이 입력되니, (제일 끝 값 = 제일 큰 값)으로 size 넘긴다
int mul = 1;
bool flag = 0; //한번이라도 소수가 나왔다면 flag = true
for (int i = 0; i < N; i++) {
if (PN.at(vec1[i]) == false) {
flag = true;
mul *= vec1[i];
PN.at(vec1[i]) = true; //한 번 거친 소수는 다시 출력하지 않는다
}
}
if (flag == 0) cout << -1;
else cout << mul;
return 0;
} 실행 결과 다음과 같이 런타임 에러-Out Of range오류가 생기는 것을 확인했습니다. |
Beta Was this translation helpful? Give feedback.
Answered by
ghdcksgml1
May 3, 2022
Replies: 1 comment
-
틀린 부분이 2부분 있습니다.
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ghdcksgml1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
틀린 부분이 2부분 있습니다.