오일러 프로젝트 17번
1부터 5까지의 숫자를 영어로 쓰면 one, two, three, four, five 이고,
각 단어의 길이를 더하면 3 + 3 + 5 + 4 + 4 = 19 이므로 사용된 글자는 모두 19개입니다.
1부터 1,000까지 영어로 썼을 때는 모두 몇 개의 글자를 사용해야 할까요?
참고: 빈 칸이나 하이픈('-')은 셈에서 제외하며, 단어 사이의 and 는 셈에 넣습니다.
예를 들어 342를 영어로 쓰면 three hundred and forty-two 가 되어서 23 글자,
115 = one hundred and fifteen 의 경우에는 20 글자가 됩니다.
#include <stdio.h>
int main (void){
int hundred = 7;
int thousand = 8;
int and = 3;
int arr1[20] = {0,3,3,5,4,4,3,5,5,4,3, 6,6,8,8,7,7,9,8,8}; // 0~ 19
int arr2[10] = {0,0,6,6,5,5,5,7,6,6};//
int i, sum =0;
for ( i = 1; i <=1000; i++){
int t = i/1000;
int h = (i/100)%10;
int te = (i%100>=20)?(i/10)%10:0;
int s = (i%100>=20)?(i%10):(i%100);
int wr = 0;
if(t) wr += arr1[t] + thousand;
if(h) wr += arr1[h] + hundred;
if(te) wr += arr2[te];
if(s) wr += arr1[s];
if( (t|h) && (te|s)) wr += and;
sum += wr;
}
printf("결과값 = %d \n", sum);
return 0;
}