μ΄ ν¬μ€ν
μ μΏ ν‘ ννΈλμ€ νλμ μΌνμΌλ‘, μ΄μ λ°λ₯Έ μΌμ μ‘μ μμλ£λ₯Ό μ 곡λ°μ μ μμ΅λλ€.
λ°μν
Problem 17 17 May 2002 If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
package com.tistory.honeybox.Euler; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class Euler17 { static String getTenUnit(int num) { switch(num) { case 0: return "ten"; case 1: return "eleven"; case 2: return "twelve"; case 3: return "thirteen"; case 4: return "fourteen"; case 5: return "fifteen"; case 6: return "sixteen"; case 7: return "seventeen"; case 8: return "eighteen"; case 9: return "nineteen"; } return null; } static String getNum(int num) { switch(num) { case 0: return ""; case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; } return null; } static String getJarisu(int size,int num) { switch(size) { case 4: //μ²μ리 return getNum(num)+"thousand"; case 3: //λ°±μ리 case 0: switch(num) { case 0:return ""; default:return getNum(num)+"hundred"; } case 2: //μμ리 switch(num) { case 0: return ""; case 1: return ""; case 2: return "twenty"; case 3: return "thirty"; case 4: return "forty"; case 5: return "fifty"; case 6: return "sixty"; case 7: return "seventy"; case 8: return "eighty"; case 9: return "ninety"; } case 1: return getNum(num); default: return null; } } static String readInEnglish(String strNum) { String ret=""; for(int i = 0 ; i < strNum.length() ; i++) { if(strNum.length()-i==2 && (Integer.parseInt(strNum.charAt(i)+"")==1)) { ret+=getTenUnit(Integer.parseInt(strNum.charAt(i+1)+"")); return ret; } ret +=getJarisu(strNum.length()-i ,Integer.parseInt(strNum.charAt(i)+"")); if(strNum.length()-i==3) { if(strNum.charAt(strNum.length()-(i+1))!= '0' || strNum.charAt(strNum.length()-(i+2))!= '0') { ret+="and"; } } } return ret; } public static void main(String [] args) throws FileNotFoundException { PrintWriter out = new PrintWriter(new File("out")); int total=0; StringBuffer strBuffer = new StringBuffer(); for(int i = 1 ; i <= 1000 ;i++) { String temp = readInEnglish(i+""); strBuffer.append(temp); total += temp.trim().length(); System.out.print(temp); out.println(temp); } System.out.println(strBuffer); System.out.print("μ λ΅: " +total); } }
λ°μν