Project Eluer - 17

Number letter counts

Problem 17

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.

翻译:数字1到5的英文是:one, two , three, four, five ,所有英文字母个数和为:3+3+5+4+4 = 19。 那么从1到1000,所有英文字母个数和为多少?

note:不包括空格和短横线 , 例如: 342:(three hundred and forty-two) 一共23个字符, 115(one hundred and fifteen)包含20个字符。使用“and”是依照英式英语的语法(在千或百 到 十或个位之间要添加“and”,但是此时十位和个位不能同时为0,比如:100 为 one hundred, 没有and。 101为 one hundred and one.

代码:

package projectEuler;

public class Problem17 {
	private static int[] numbers = new int[100];

	public static void main(String[] args) {
		int sum = 0;
		init_numbers();
		for(int i=1; i<=1000; i++){
			sum += analysis_num(i);
		}
		System.out.println("sum:"+sum);
//		System.out.println(""+analysis_num(342));
	}

	private static int analysis_num(int num) {
		int[] wei = new int[4];
		wei[0] = num/1000;  //千位
		wei[1] = (num-wei[0]*1000)/100; //百位
		wei[2] = (num-wei[0]*1000 - wei[1]*100) / 10; //十位
		wei[3] = num%10; //个位

		int[] wordsNumber = new int[4];
		wordsNumber[0] = wei[0]>0? (numbers[wei[0]]+8) : 0; // 千位字母数 = 数字 + thousand
		wordsNumber[1] = wei[1]>0? (numbers[wei[1]]+7) : 0; // 百位字母数 = 数字 + hundred

		if( wei[2] >= 2 ){
			wordsNumber[2] = numbers[wei[2]*10];
			wordsNumber[3] = numbers[wei[3]];
		}
		else{
			wordsNumber[2] = 0;
			wordsNumber[3] = numbers[wei[2]*10+wei[3]];
		}

		int sum = 0;
		for(int i=0; i<wordsNumber.length; i++){
			sum += wordsNumber[i];
		}
		if( (wei[1]>0 || wei[1]>0) && (wei[2]>0 || wei[3]>0)){ // 如果有千位百位,十位和个位
			sum += 3;
		}

		return sum;
	}

	private static void init_numbers() {
		numbers[0] = 0;// no pronunciation for zero
		numbers[1] = 3;// "one"
		numbers[2] = 3;// "two"
		numbers[3] = 5;// "three"
		numbers[4] = 4;// "four"
		numbers[5] = 4;// "five"
		numbers[6] = 3;// "six"
		numbers[7] = 5;// "seven"
		numbers[8] = 5;// "eight"
		numbers[9] = 4;// "nine"
		numbers[10] = 3;// "ten"
		numbers[11] = 6;// "eleven"
		numbers[12] = 6;// "twelve"
		numbers[13] = 8;// "thirteen"
		numbers[14] = 8;// "fourteen"
		numbers[15] = 7;// "fifteen"
		numbers[16] = 7;// "sixteen"
		numbers[17] = 9;// "seventeen"
		numbers[18] = 8;// "eighteen"
		numbers[19] = 8;// "nineteen"
		numbers[20] = 6;// "twenty"
		numbers[30] = 6;// "thirty"
		numbers[40] = 5;// "forty"
		numbers[50] = 5;// "fifty"
		numbers[60] = 5;// "sixty"
		numbers[70] = 7;// "seventy"
		numbers[80] = 6;// "eighty"
		numbers[90] = 6;// "ninety"
	}

}

这道题其实不是很难,主要是逻辑有点复杂。然后看init_numbers() 这个函数,这个是我看到别人这样用才用的,就是用数组的下边来表示这个数是几,这个比较巧妙。

比如 111,算法中可以看到我会先分离出千位,百位,十位,个位。

千位的字母个数: 数字 + thousand ->    numbers[ 千位数字 ] + 8

十位和个位需要判断: 如果大于20, 则需要单独分离出十位,再与个位字母数相加。如果小于20, 直接得到 numbers[数字].

好了就讲到这里,代码里有注释,参照看一下就可以了。

时间: 2024-08-24 10:20:49

Project Eluer - 17的相关文章

Project Eluer - 18

Maximum path sum I Problem 18 By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top t

Project Eluer - 21

Amicable numbers Problem 21 Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numb

Project Eluer - 22

题目: Names scores Problem 22 Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, mul

Project Eluer - 23

Non-abundant sums Problem 23 A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect nu

九度oj 题目1499:项目安排

题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的.由于小明马上就要硕士毕业了,面临着买房.买车.给女友买各种包包的鸭梨,但是他的钱包却空空如也,他需要足够的money来充实钱包.万能的网友麻烦你来帮帮小明,如何在最短时间内安排自己手中的项目才能保证赚钱最多(注意:做项目的时候,项目不能并行,即两个项目之间不能有时间重叠,但是一个项目刚结束,就可以立即做另一个项目,即项目起止时间点可以重叠). 输

PE1 Multiples of 3 and 5

false 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-

一个高级的makefile文件

该Makefile适用于最后生成若干个可执行文件的小型C/C++工程,只要将该Makefile放在源码根目录下make,它会自动查找该目录下(包括子目录)的所有源码文件(支持.cpp .c .h格式)并自动生成正确的依赖关系,并且为了不污染源码文件夹,源码和编译过程中的中间文件会分开(Debug模式编译生成的中间文件在Debug目录下,Release模式在Release目录下). 1 ########################################################

Team Foundation Server 2013 with Update 3 Install LOG

[Info   @10:14:58.155] ====================================================================[Info   @10:14:58.163] Team Foundation Server Administration Log[Info   @10:14:58.175] Version  : 12.0.30723.0[Info   @10:14:58.175] DateTime : 10/03/2014 18:1

--------Hibernate框架之双向多对多关系映射

今天跟大家分享下在Hibernate中双向的多对多的映射关系 这次我们以项目和员工举个栗子,因为大家可以想象得到,在真实的环境下,一个项目肯定是对应着多个员工的,这毫无疑问, 那么同时,一个比较牛员工也能同时参与多个项目的开发,这就体现了双向多对多的关系. 首先呢,我们得弄清楚在底层数据库中表与表之间的关系,我们创建一个员工表(Employee)和项目表(Project)毫无疑问,那么我们要怎么体现出多对多的关系呢? 当然有很多种方法,这里我以单独提出一张关系表为例,也就是说,我单独创建一张表来