0
1에서 5까지 숫자가 1,5 개일 경우 1,2,316,41,5 개로 쓰면 3 + 3 + 5 + 4 + 4 = 19 문자가 사용됩니다 전체적으로.프로젝트 오일러 # 17 오답 대답
1에서 1000 (1000)까지의 숫자가 모두 이면 얼마나 많은 문자가 사용됩니까?
참고 : 공백이나 하이픈을 넣지 마십시오. 예를 들어, 342 ( 300 개)에는 23 개의 문자가 포함되어 있고 115 (1515) 개의 에는 20 개의 문자가 들어 있습니다. 숫자를 쓸 때 "및"을 사용하면 에 영국 사용을 준수합니다.
내 코드
public class ProjectEuler17 {
public static String[] ones = { "", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
public static String[] tens = { "", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
public static String[] hundreds = { "", "onehundred", "twohundred",
"threehundred", "fourhundred", "fivehundred", "sixhundred",
"sevenhundred", "eighthundred", "ninehundred", "oneThousand" };
public static void main(String[] args) {
System.out.println(run());
}
public static String run() {
int sum = 0;
for (int i = 1; i <= 1000; i++)
sum += convertToWord(i).length();
return Integer.toString(sum);
}
public static String convertToWord(int n) {
int unit=n%10;
int tensdivide = (n/10)%10;
int hundreadsdivide = n/100;
int hundredModulo=n%100;
if (n <= 19) {
//under 20(exclusive)
return ones[n];
} else if (n < 100 && n > 19) {
//from 20 to 100(exclusive)
return tens[tensdivide] + ones[unit];
} else {
/* 100,200,300,400,500 ...1000("onehundred", "twohundred","threehundred", "fourhundred", "fivehundred", "sixhundred",
"sevenhundred", "eighthundred", "ninehundred", "oneThousand") */
if(hundredModulo == 0){
return hundreds[hundreadsdivide] +tens[tensdivide] + ones[unit];
}else{
//one hundred and tewnty
return hundreds[hundreadsdivide] +"and" +tens[tensdivide] + ones[unit];
}
}
}
내가 잘못 21088과 같은 답을 얻고 이하 정답은 : u는 어떻게 만드는 잘못도 저를 제안 어떤 것을 발견하면 21,124 것은 도와주세요 내 코드가 빠릅니다.
+1 재미있는 질문을하고 잘 시도해보십시오. –
어떻게 '21124'를 계산 했습니까? 배열에서 문자열의 철자를 검사 했습니까? –
@Ninad Pingale - 처음에 나는 21125를 얻었습니다. 그 다음 오일러 사이트에 그 대답을 입력했습니다. 정확하지 않아서 net에서 확인했습니다. 그리고 정답은 21124입니다. – Kiranaditya