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

原创 2012年03月21日 13:33:40

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

UVa Problem 10213 How Many Pieces of Land? (土地分割)

// How Many Pieces of Land? (土地分割) // PC/UVa IDs: 110602/10213, Popularity: B, Success rate: average...

Problem 19 of How many Sundays fell on the first of the month during the twentieth century

http://projecteuler.net/problem=19 You are given the following information, but you may prefer to...

If not, then the process of what would be my future

If not, then the process of what would be my future I was born in a not very wealthy family, was no...

How many permissions used in the famous IM app?

Permissions in the famurse IM app.
  • shinpox
  • shinpox
  • 2014年05月04日 22:29
  • 697

在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)

P.S. 之前在一篇文章中说过VS2005下面_countof的实现,最后提到过为什么使用模板而抛弃之前的实现,后来在网上看到了这篇文章,感觉讲得比较清楚,就翻译了一下,还算简单清楚。 ...

Find Out How Many File Descriptors Are Being Used

Linux: Find Out How Many File Descriptors Are Being Used by NIX CRAFT on AUGUST 21, 2007 · 14 ...

How Can XML be Used?

How Can XML be Used? XML is used in many aspects of web development, often to simplify data storage...
  • Koupoo
  • Koupoo
  • 2011年08月17日 09:19
  • 424

(Pod造成的)One of the two will be used. Which one is undefined.

前因 因为项目里,需要一个图表控件。项目的图表部分不是重点,没必要花费大量时间自己去写一个,所以去找第三方的图表。 安卓使用的是一个功能比较强大的第三方MPAndroidChart。ios里没有功能...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:欧拉项目 Problem 17 of how many letters would be used?
举报原因:
原因补充:

(最多只允许输入30个字)