https://www.acmicpc.net/problem/1929
입출력
풀이
#include<iostream>
#include<cmath>
using namespace std;
bool find(int num)
{
for (int j = 2; j <= sqrt(num); j++)
{
if ((num) % j == 0)
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int m, n;
cin >> m >> n;
for (int i = m; i <= n; i++)
{
if (i < 2)
{
cout << "2" << "\n";
i = 2;
}
else
{
while (find(i))
{
i++;
}
if(i <= n)
cout << i << "\n";
}
}
return 0;
}
백준 4134번 다음 소수 에서 했던 함수를 그대로 이용하면 된다
출력 부분에서 i가 n보다 작거나 같다는 조건을 넣어줘야한다
처음에 틀렸을 때의 반례
입력 : 1 / 10
잘못된 출력 : 2 / 3/ 5/ 7/ 11
제대로된 출력 : 2/3/5/7
'> 코딩테스트' 카테고리의 다른 글
[백준] 26069 붙임성 좋은 총총이 (C++) (0) | 2023.07.03 |
---|---|
[백준] 4948 베르트랑 공준 (C++) (0) | 2023.06.24 |
[백준] 4134 다음 소수 (C++) (0) | 2023.06.22 |
[백준] 1735 분수 합 (C++) (0) | 2023.06.20 |
[백준] 1269 대칭 차집합 (C++) (0) | 2023.06.17 |