Development Project

[ Baekjoon - 09/16 ] - 2153번: 소수 단어 본문

CodingTest/Baekjoon

[ Baekjoon - 09/16 ] - 2153번: 소수 단어

나를 위한 시간 2022. 9. 16. 23:23
 

2153번: 소수 단어

소수란 1과 자기 자신으로만 나누어떨어지는 수를 말한다. 예를 들면 1, 2, 3, 5, 17, 101, 10007 등이 소수이다. 이 문제에서는 편의상 1도 소수로 하자. 알파벳 대소문자로 이루어진 영어 단어가 하나

www.acmicpc.net

 

  • 소요시간 : 30분

 

  • 정답코드
import java.util.*;
import java.io.*;

public class Main{
    static String input;
    static int num=0;
    static boolean[] isNotPrime; // 52*20 = 1040

    public static void main(String[] args) throws IOException {
        //System.setIn(new FileInputStream("src/input.txt"));

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		//에라토스테네스의 체 구현
        isNotPrime = new boolean[1041];
        for (int i = 2; i < 1041; i++) {
            if(!isNotPrime[i]){
                for (int j = 2; i*j < 1041; j++) {
                    isNotPrime[i*j] = true;
                }
            }
        }

		//문자열(알파벳)을 입력받고 각 문자열에 배정된 수를 더함
        input = br.readLine();
        for (int i = 0; i < input.length(); i++) {
            if(Character.isUpperCase(input.charAt(i))){
                num= num+input.charAt(i)-'A'+27;
            }else{
                num= num+input.charAt(i)-'a'+1;
            }
        }
        System.out.println(isNotPrime[num]?"It is not a prime word.":"It is a prime word.");
    }
}
Comments