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, multiply this value by its alphabetical
position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

翻译:

题目给出文件 names.txt ,里面存放的是 5000多个名字。我们的工作有3步,第一步:将所有名字按照字典顺序排序。第二步:算出每个名字的位置值。计算方法:按照每个字母在字母表中的顺序,A:1,B:2....,算出一个名字所有字母的和,然后用这个值乘以当前名字所在的位置,比如“COLIN",所有字母和为:3+15+12+9+14=53, 再乘以名字排序后的位置:938得到 53*938=49714,为”COLIN"的位置值。第三步:求所有名字的位置值相加的和。

思路:

说了这么大一堆,就是说需要把names.txt中的所有名字读出来,放到String数组中,然后将数组排序,就可以得到排序过后的名字列表。然后算每个名字的字母值的和乘以当前的索引就求得了这个名字的位置值,然后将所有名字的位置值相加,搞定!(位置值是我自己的说法,大家不用在意)

代码:

package projectEuler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class Problem22 {
	public static void main(String[] args) {
		File file = new File("F://", "names.txt");
		ArrayList<String> listStr = new ArrayList<String>();
		int result = 0;

		if (null != file) {
			try {
				FileReader fr = new FileReader(file);
				StringBuffer sb = new StringBuffer();
				int ch;
				//这里条件 ">=-1"主要考虑到names.txt 的最后一个名字后面没有",",所有读到末尾的时候还应该添加一次名字到list中
				while((ch = fr.read()) >= -1){
					if (ch == ',' || ch == -1) {
						listStr.add(sb.substring(1, sb.length() - 1));
						sb.delete(0, sb.length());
						if (ch == -1) {
							break;
						}
						continue;
					}
					sb.append((char)ch);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			//直接采用java内部的排序,省去排序麻烦,速度也快
			Collections.sort(listStr);
			int listLen = listStr.size();
			for (int i = 0; i < listLen; i++) {
				result += countStrValue(i, listStr.get(i));
			}

			System.out.print("result:" + result);
		}
	}

	/**
	 * 计算每个字符串的位置值
	 * @param index string所在位置
	 * @param str string字符串
	 */
	public static int countStrValue(int index, String str) {
		int len = str.length();
		char ch;
		int sum = 0;
		for (int i = 0; i < len; i++) {
			ch = str.charAt(i);
			//都是大写字母,所以可以直接减64
			sum += (int) (ch - 64);
		}
		return sum * (index + 1);
	}
}

代码里面有注释,这里就不做解释了,很简单。

本来想读取文件中的字符串到ArrayList中是一个字母一个字母读取,会花费很多时间,花了很多时间想优化,也没有找到好的方法。

然后还有一个问题就是,很多人肯定会被字符串排序那儿难倒,毕竟比较次数那么多,要考虑性能的话还是有一定难度,所以我选择了java自带的排序,这个太偷懒了,不值得借鉴哦,还是自己写一个吧!

时间: 2024-12-30 03:35:04

Project Eluer - 22的相关文章

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 - 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 - 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 w

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

共有31款PHP 图形/图像处理开源软件(转)

详情点击:http://www.oschina.net/project/lang/22/php?tag=141&os=0&sort=view PHP 图像处理库 Grafika Grafika 是一个 PHP 的图像处理库.可用于改变图像大小.剪切.比较,以及添加水印等操作.同时可以创建一些文本图片.几何图形并应用一些过滤器.基于 Imagick 和 GD 构建. 特征 智能裁剪 图像比较 感知哈希 高级图像处理过滤器 贝塞尔曲线 示例代码: ...更多Grafika信息 最近更新:Gra

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-

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

负载均衡 、 静态缓存

upstream project { server 22.22.22.2:3000; server 22.22.22.3:3000; server 22.22.22.5:3000; } server { listen 80; location / { proxy_pass http://project; } location ~* \.(css|js|gif|jpe?g|png)$ { expires 168h; } location /api { expires 10m; } }

一个创建Coco2d-x项目的脚本

1.使用环境 我测试的环境是Mac OS 10.10 +Coco2d-x 3.2,是使用shell写的脚本,应该linux/unix都应该 可以使用. 2.使用可能出现的问题 使用中可能会爆权限不足的错误,给两个文件赋予权限就可以,以Mac 为例,打开终端 cd到该文件目 录执行命令:chmod u+x  文件名.后缀 ,需要给两个文件附加这种权限,一个是coco2d-x创建项目的脚本 (cocos.py),另一个是下面提供的这个脚本. 3.配置参数 打开脚本,前四个参数需要自己配置的 cfRo