알고리즘 및 자료구조/문제
백준알고리즘 2153번 소수 단어
ktko
2018. 5. 14. 12:07
백준알고리즘
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(); } }