Problem22

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?

package com.yao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: shuimuqinghua77
* Date: 12-3-13
* Time: 下午9:04
*/
public class Problem22 {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\Administrator\\Desktop\\names.txt");

///home/yaoyao/template/names.txt
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String namestxt=br.readLine();
br.close();
fr.close();
String[] names=namestxt.split("\",\"");
/*去除最后一个和第一个name的引号*/
names[0]=names[0].substring(1, names[0].length());
names[names.length-1]=names[names.length-1].substring(0,names[names.length-1].length()-1);
/**建立一个链表 以字典序列存储名字*/
List<String> list=new LinkedList<String>();
int[] location=new int[26];
long start=System.currentTimeMillis();
for(String name:names){
if(list.size()==0){
location[name.charAt(0)-'A']++;
list.add(name);
continue;
}
int bigger=0;
int insert=0;
//获取索引位置
int i=0;
int x=name.charAt(0)-'A';
for(int k=0;k<=x-1;k++){
i+=location[k];
}
for(;i<list.size();i++){
String pre=list.get(i);
int len=pre.length()>name.length()? name.length():pre.length();
for(int j=0;j<len;j++){
if(pre.charAt(j)>name.charAt(j)){
bigger=1;
break;
}
else if(pre.charAt(j)<name.charAt(j)){
bigger=0;
break;
}
else{
bigger=2;
}
}
if(bigger==1||(bigger==2&&(pre.length()>name.length()))){
location[name.charAt(0)-'A']++;
list.add(i,name);
insert=1;
break;
}
}
if(insert==0)list.add(name);

}
int sum=0;
int a='A'-1;
for(int i=0;i<list.size();i++){
String name=list.get(i);
int base=0;
for(int j=0;j<name.length();j++){
base+=name.charAt(j)-a;
}
sum+=base*(i+1);
}
System.out.println(sum);
long end=System.currentTimeMillis();
System.out.println(end-start);

}

}


package com.yao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: shuimuqinghua77
* Date: 12-3-13
* Time: 下午9:04
*/
public class Problem22 {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\Administrator\\Desktop\\names.txt");

///home/yaoyao/template/names.txt
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String namestxt=br.readLine();
br.close();
fr.close();
String[] names=namestxt.split("\",\"");
/*去除最后一个和第一个name的引号*/
names[0]=names[0].substring(1, names[0].length());
names[names.length-1]=names[names.length-1].substring(0,names[names.length-1].length()-1);
long start=System.currentTimeMillis();
Arrays.sort(names);
/* *//**建立一个链表 以字典序列存储名字*//*
List<String> list=new LinkedList<String>();
int[] location=new int[26];
long start=System.currentTimeMillis();
for(String name:names){
if(list.size()==0){
location[name.charAt(0)-'A']++;
list.add(name);
continue;
}
int bigger=0;
int insert=0;
//获取索引位置
int i=0;
int x=name.charAt(0)-'A';
for(int k=0;k<=x-1;k++){
i+=location[k];
}
for(;i<list.size();i++){
String pre=list.get(i);
int len=pre.length()>name.length()? name.length():pre.length();
for(int j=0;j<len;j++){
if(pre.charAt(j)>name.charAt(j)){
bigger=1;
break;
}
else if(pre.charAt(j)<name.charAt(j)){
bigger=0;
break;
}
else{
bigger=2;
}
}
if(bigger==1||(bigger==2&&(pre.length()>name.length()))){
location[name.charAt(0)-'A']++;
list.add(i,name);
insert=1;
break;
}
}
if(insert==0)list.add(name);

}*/
int sum=0;
int a='A'-1;
for(int i=0;i<names.length;i++){
String name=names[i];
int base=0;
for(int j=0;j<name.length();j++){
base+=name.charAt(j)-a;
}
sum+=base*(i+1);
}
System.out.println(sum);
long end=System.currentTimeMillis();
System.out.println(end-start);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值