Algorithm/BOJ

C#) [BOJ] 1978 소수찾기

HSH12345 2023. 1. 25. 22:20

1978번: 소수 찾기 (acmicpc.net)

 

1978번: 소수 찾기

첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _02
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int N = int.Parse(sr.ReadLine());
            if(N <= 100)
            {
                int[] arr = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
                int cnt = 0;
                
                for (int i = 0; i < N; i++)
                {
                    bool isDecimal = false;

                    if (arr[i] != 1 && arr[i] <= 1000)
                    {
                        for(int j = 2; j < arr[i]; j++)
                        {
                            if (arr[i] % j == 0)
                            {
                                isDecimal = true;
                                break;
                            }                            
                        }

                        if (!isDecimal) cnt++;
                    }                    
                }

                sw.WriteLine(cnt);
            }

            sr.Close();
            sw.Close();
        }
    }
}