关闭

欧拉项目 Problem 17 of how many letters would be used?

493人阅读 评论(0) 收藏 举报

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.




public class BritishLetter {
	public static final String ONE="one";
	public static final String TWO="two";
	public static final String THREE="three";
	public static final String FOUR="four";
	public static final String FIVE="five";
	public static final String SIX="six";
	public static final String SEVEN="seven";
	public static final String EIGHT="eight";
	public static final String NINE="nine";
	public static final String TEN="ten";
	public static final String ELEVEN="eleven";
	public static final String TWELVE="twelve";
	public static final String THIRTEEN="thirteen";
	public static final String FOURTEEN="fourteen";
	public static final String FIFTEEN="fifteen";
	public static final String SIXTEEN="sixteen";
	public static final String SEVENTEEN="seventeen";
	public static final String EIGHTEEN="eighteen";
	public static final String NINETEEN="nineteen";
	public static final String TWENTY="twenty";
	public static final String THIRTY="thirty";
	public static final String FORTY="forty";
	public static final String FIFTY="fifty";
	public static final String SIXTY="sixty";
	public static final String SEVENTY="seventy";
	public static final String EIGHTY="eighty";
	public static final String NINETY="ninety";
	public static final String HUNDRED="hundred";
	public static final String AND_LENGTH="and";
	public static final String THOUSAND="thousand";
	
	public static void main(String args[]){
		int sum=0;
		for(int i=1;i<1000;i++){
			if(i<10){
				sum+=numUnit(i);
				System.out.println("long="+numUnit(i)+",i="+i);
			}else if(i>=10&&i<100){
				sum+=numDecade(i);
				System.out.println("long=="+numDecade(i)+",i="+i);
			}else if(i>=100){
				sum+=numHundred(i);
				System.out.println("long==="+numHundred(i)+",i="+i);
			}
		}
		System.out.println(sum+THOUSAND.length()+ONE.length());
		System.out.println(numHundred(342));
		System.out.println(numHundred(115));

	}
	
	public static int numUnit(int n){
		int num=0;
		if(n==1){
			num=ONE.length();
		}else if(n==2){
			num=TWO.length();
		}else if(n==3){
			num=THREE.length();
		}else if(n==4){
			num=FOUR.length();
		}else if(n==5){
			num=FIVE.length();
		}else if(n==6){
			num=SIX.length();
		}else if(n==7){
			num=SEVEN.length();
		}else if(n==8){
			num=EIGHT.length();
		}else if(n==9){
			num=NINE.length();
		}
		return num;
	}
	
	public static int numDecade(int n){
		int num=0;
		if(n/10==0){
			num=numUnit(n);
		}else if(n/10==1){
			if(n==10){
				num=TEN.length();
			}else if(n==11){
				num=ELEVEN.length();
			}else if(n==12){
				num=TWELVE.length();
			}else if(n==13){
				num=THIRTEEN.length();
			}else if(n==14){
				num=FOURTEEN.length();
			}else if(n==15){
				num=FIFTEEN.length();
			}else if(n==16){
				num=SIXTEEN.length();
			}else if(n==17){
				num=SEVENTEEN.length();
			}else if(n==18){
				num=EIGHTEEN.length();
			}else if(n==19){
				num=NINETEEN.length();
			}
		}else if(n/10==2){
			num=TWENTY.length()+numUnit(n%10);
		}else if(n/10==3){
			num=THIRTY.length()+numUnit(n%10);
		}else if(n/10==4){
			num=FORTY.length()+numUnit(n%10);
		}else if(n/10==5){
			num=FIFTY.length()+numUnit(n%10);
		}else if(n/10==6){
			num=SIXTY.length()+numUnit(n%10);
		}else if(n/10==7){
			num=SEVENTY.length()+numUnit(n%10);
		}else if(n/10==8){
			num=EIGHTY.length()+numUnit(n%10);
		}else if(n/10==9){
			num=NINETY.length()+numUnit(n%10);
		}
		return num;
	}
	
	public static int numHundred(int n){
		int num=0;
		if(n/100==1&&n!=100){
			num=ONE.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==2&&n!=200){
			num=TWO.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==3&&n!=300){
			num=THREE.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==4&&n!=400){
			num=FOUR.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==5&&n!=500){
			num=FIVE.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==6&&n!=600){
			num=SIX.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==7&&n!=700){
			num=SEVEN.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==8&&n!=800){
			num=EIGHT.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n/100==9&&n!=900){
			num=NINE.length()+HUNDRED.length()+AND_LENGTH.length()+numDecade(n%100);
		}else if(n==100){
			num=ONE.length()+HUNDRED.length();
		}else if(n==200){
			num=TWO.length()+HUNDRED.length();
		}else if(n==300){
			num=THREE.length()+HUNDRED.length();
		}else if(n==400){
			num=FOUR.length()+HUNDRED.length();
		}else if(n==500){
			num=FIVE.length()+HUNDRED.length();
		}else if(n==600){
			num=SIX.length()+HUNDRED.length();
		}else if(n==700){
			num=SEVEN.length()+HUNDRED.length();
		}else if(n==800){
			num=EIGHT.length()+HUNDRED.length();
		}else if(n==900){
			num=NINE.length()+HUNDRED.length();
		}
		return num;
	}
}

Answer:
21124

http://projecteuler.net/problem=17

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:173057次
    • 积分:2955
    • 等级:
    • 排名:第12429名
    • 原创:57篇
    • 转载:36篇
    • 译文:31篇
    • 评论:60条
    最新评论