https://www.acmicpc.net/problem/2581
2581번: 소수
M이상 N이하의 자연수 중 소수인 것을 모두 찾아 첫째 줄에 그 합을, 둘째 줄에 그 중 최솟값을 출력한다. 단, M이상 N이하의 자연수 중 소수가 없을 경우는 첫째 줄에 -1을 출력한다.
www.acmicpc.net
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <cmath>
#include <functional>
#include <string>
#include <sstream>
#include <numeric>
#include <stack>
using namespace std;
void fastIO();
int main()
{
int M, N = 0;
int cnt = 0;
vector<int> vec;
cin >> M;
cin >> N;
for (auto i = M; i <= N; i++)
{
for (auto j = 1; j <= i; j++)
{
if (i % j == 0)
cnt++;
}
if (cnt == 2)
{
vec.push_back(i);
}
cnt = 0;
}
if (vec.empty())
{
cout << "-1" << endl;
}
else {
cout << accumulate(vec.begin(), vec.end(), 0) << endl;
cout << *min_element(vec.begin(), vec.end()) << endl;
}
}
void fastIO()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
'STUDY > Baekjoon' 카테고리의 다른 글
[백준] 2587 c++ 대표값2 (0) | 2023.03.14 |
---|---|
[백준] 2798 c++ 블랙잭 (0) | 2022.08.25 |
[백준] 2869 c++ 달팽이는 올라가고 싶다 (0) | 2022.08.25 |
[백준] 1316 c++ 그룹 단어 체커 (0) | 2022.08.25 |
[백준] 25304 c++ 영수증 (0) | 2022.08.24 |