欧拉项目 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

<shader>编程中遇到的问题-too many texture interpolators would be used for ForwardBase pass

今天在进行地形着色器纹理混合的编程过程中,出现了一个报错 后来才发现是,因为在脚本中Input里设置的材质的uv变量过多,当前版本的shader model不支持造成的。将shader mode...
  • zys91011_muse
  • zys91011_muse
  • 2017年07月22日 21:20
  • 417

[Unity 纠错] Too many texture interpolators would be used for ForwardBase pass at line

在unity console中错误信息如下 :  Shader error in 'Custom/L8_Sky': Too many texture interpolators would be u...
  • hunter_hb
  • hunter_hb
  • 2014年09月17日 20:40
  • 3613

Too many texture interpolators would be used for ForwardBase pass

这是由于Input中定义的材质uv变量过多,当前版本的shader model不支持造成的。 将shader model改为更高的版本即可解决。比如我现在用的#pragma target 3.0...
  • jueane
  • jueane
  • 2016年09月19日 09:23
  • 1093

欧拉计划译文及答案(11-20)

翻译&解答:Meili (11)方阵中的最大积 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 8...
  • Nithouson
  • Nithouson
  • 2017年01月26日 12:32
  • 1017

ocp-258

QUESTION NO: 258 How many database instances are used during a database-duplication process?A. One ...
  • xuejiayue1105
  • xuejiayue1105
  • 2015年10月09日 14:20
  • 881

CCNU 2010新生练习赛(DP)[差很多的解题报告]

话说这套题这么久了都没有搞定,喵呜大神说简单的都不屑于做的题,就是做不出来。。orz 膜拜喵呜大神。我现在的水平比喵呜去年这个时候差太多了,再次膜拜。SSY同学很用心,望一起努力。其他同学也是啊。只写...
  • LaputaStar
  • LaputaStar
  • 2011年05月02日 02:09
  • 1040

DP问题:leetcode(96) Unique Binary Search Trees

问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? ...
  • u014514939
  • u014514939
  • 2018年01月23日 00:50
  • 13

Problem 17 - How many letters would be needed to write all the numbers in words from 1 to 1000?

 If the numbers 1 to 5 are written out in words: one, two, three,four, five, then there are 3 + 3 + ...
  • No_End_Point
  • No_End_Point
  • 2008年07月17日 22:12
  • 1891

HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)

Problem Description The Romans used letters from their Latin alphabet to represent each of the seve...
  • qq_26525215
  • qq_26525215
  • 2016年06月05日 01:29
  • 4690

HDU:1213 How Many Tables(简单并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • zugofn
  • zugofn
  • 2016年08月01日 20:20
  • 542
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:欧拉项目 Problem 17 of how many letters would be used?
举报原因:
原因补充:

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