https://www.acmicpc.net/problem/2581
2581번: 소수
M이상 N이하의 자연수 중 소수인 것을 모두 찾아 첫째 줄에 그 합을, 둘째 줄에 그 중 최솟값을 출력한다. 단, M이상 N이하의 자연수 중 소수가 없을 경우는 첫째 줄에 -1을 출력한다.
www.acmicpc.net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _230206_1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int m = int.Parse(sr.ReadLine());
int n = int.Parse(sr.ReadLine());
List<int> decimalList = new List<int>();
for (int i = m; i < n + 1; i++)
{
bool isDecimal = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
isDecimal = false;
break;
}
}
if (isDecimal && i != 1) decimalList.Add(i);
}
if (decimalList.Count > 0)
{
sw.WriteLine(decimalList.Sum());
sw.WriteLine(decimalList.Min());
}
else sw.WriteLine(-1);
sr.Close();
sw.Close();
}
}
}
1을 소수에 포함시키고 있어 반례를 찾아보았던 것만 빼면 쉬운 문제였다. 소수관련 문제가 나올 때는 반드시 1을 소수에 포함시키지 않도록 의식하고 있어야한다.
'Algorithm > BOJ' 카테고리의 다른 글
C#) [BOJ] [BOJ] 1297 TV 크기 (0) | 2023.02.08 |
---|---|
C#)[BOJ] 1764 듣보잡 ★ (0) | 2023.02.06 |
C#) [BOJ] 1260 DFS와 BFS ★ (0) | 2023.02.05 |
[BOJ] 2605 줄 세우기 (0) | 2023.02.05 |
C#) [BOJ] 5524 입실 관리 (0) | 2023.02.04 |