본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

백준알고리즘


https://www.acmicpc.net/problem/2153


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String inputStr = scan.nextLine().trim();
        
        int strTotal = 0;
        
        for(int i=0; i < inputStr.length(); i++) {
            char ch = inputStr.charAt(i);
            if(ch >= 'A' && ch <= 'Z') {
                strTotal += (int)ch - 38;
            } else if (ch >= 'a' && ch <= 'z') {
                strTotal += (int)ch - 96;
            }
        }
        
        boolean check = false;
        
        for(int i=2; i <= strTotal; i++) {
            if(i < strTotal && strTotal % i == 0) {
                break;
            }
            
            if(i == strTotal && strTotal % i ==0 ) {
                check = true;
            }
        }
        
        
        if(check || strTotal == 1) {
            System.out.println("It is a prime word.");
        } else {
            System.out.println("It is not a prime word.");
        }
        
        scan.close();
        
    }
}