Problem22

简介:

1.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.  
2.  
3.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.  
4.  
5.What is the total of all the name scores in the file? 


1.package com.yao;  
2.import java.io.BufferedReader;  
3.import java.io.File;  
4.import java.io.FileReader;  
5.import java.io.IOException;  
6.import java.util.LinkedList;  
7.import java.util.List;  
8./** 
9. * Created by IntelliJ IDEA. 
10. * User: shuimuqinghua77 
11. * Date: 12-3-13 
12. * Time: 下午9:04 
13. */  
14.public class Problem22 {  
15.    public static void main(String[] args) throws IOException {  
16.        File file=new File("C:\\Users\\Administrator\\Desktop\\names.txt");  
17.  
18.        ///home/yaoyao/template/names.txt  
19.        FileReader fr=new FileReader(file);  
20.        BufferedReader br=new BufferedReader(fr);  
21.        String namestxt=br.readLine();  
22.        br.close();  
23.        fr.close();  
24.        String[] names=namestxt.split("\",\"");  
25.        /*去除最后一个和第一个name的引号*/  
26.        names[0]=names[0].substring(1, names[0].length());  
27.        names[names.length-1]=names[names.length-1].substring(0,names[names.length-1].length()-1);  
28.        /**建立一个链表  以字典序列存储名字*/  
29.        List<String> list=new LinkedList<String>();  
30.        int[] location=new int[26];  
31.        long start=System.currentTimeMillis();  
32.        for(String name:names){  
33.            if(list.size()==0){  
34.                location[name.charAt(0)-'A']++;  
35.                list.add(name);  
36.                continue;  
37.            }  
38.            int bigger=0;  
39.            int insert=0;  
40.            //获取索引位置  
41.                int i=0;  
42.                int x=name.charAt(0)-'A';  
43.                for(int k=0;k<=x-1;k++){  
44.                 i+=location[k];  
45.                }  
46.                for(;i<list.size();i++){  
47.                String pre=list.get(i);  
48.                int len=pre.length()>name.length()? name.length():pre.length();  
49.                for(int j=0;j<len;j++){  
50.                    if(pre.charAt(j)>name.charAt(j)){  
51.                        bigger=1;  
52.                        break;  
53.                    }  
54.                    else if(pre.charAt(j)<name.charAt(j)){  
55.                        bigger=0;  
56.                        break;  
57.                    }  
58.                    else{  
59.                        bigger=2;  
60.                    }  
61.                }  
62.                if(bigger==1||(bigger==2&&(pre.length()>name.length()))){  
63.                    location[name.charAt(0)-'A']++;  
64.                    list.add(i,name);  
65.                    insert=1;  
66.                    break;  
67.                }  
68.            }  
69.            if(insert==0)list.add(name);  
70.  
71.        }  
72.        int sum=0;  
73.        int a='A'-1;  
74.        for(int i=0;i<list.size();i++){  
75.            String name=list.get(i);  
76.            int base=0;  
77.            for(int j=0;j<name.length();j++){  
78.                     base+=name.charAt(j)-a;  
79.            }  
80.             sum+=base*(i+1);  
81.        }  
82.        System.out.println(sum);  
83.        long end=System.currentTimeMillis();  
84.        System.out.println(end-start);  
85.  
86.    }  
87.  
88.} 

1.package com.yao;  
2.import java.io.BufferedReader;  
3.import java.io.File;  
4.import java.io.FileReader;  
5.import java.io.IOException;  
6.import java.util.Arrays;  
7.import java.util.LinkedList;  
8.import java.util.List;  
9./** 
10. * Created by IntelliJ IDEA. 
11. * User: shuimuqinghua77 
12. * Date: 12-3-13 
13. * Time: 下午9:04 
14. */  
15.public class Problem22 {  
16.    public static void main(String[] args) throws IOException {  
17.        File file=new File("C:\\Users\\Administrator\\Desktop\\names.txt");  
18.  
19.        ///home/yaoyao/template/names.txt  
20.        FileReader fr=new FileReader(file);  
21.        BufferedReader br=new BufferedReader(fr);  
22.        String namestxt=br.readLine();  
23.        br.close();  
24.        fr.close();  
25.        String[] names=namestxt.split("\",\"");  
26.        /*去除最后一个和第一个name的引号*/  
27.        names[0]=names[0].substring(1, names[0].length());  
28.        names[names.length-1]=names[names.length-1].substring(0,names[names.length-1].length()-1);  
29.        long start=System.currentTimeMillis();  
30.        Arrays.sort(names);  
31.    /*    *//**建立一个链表  以字典序列存储名字*//* 
32.        List<String> list=new LinkedList<String>(); 
33.        int[] location=new int[26]; 
34.        long start=System.currentTimeMillis(); 
35.        for(String name:names){ 
36.            if(list.size()==0){ 
37.                location[name.charAt(0)-'A']++; 
38.                list.add(name); 
39.                continue; 
40.            } 
41.            int bigger=0; 
42.            int insert=0; 
43.            //获取索引位置 
44.                int i=0; 
45.                int x=name.charAt(0)-'A'; 
46.                for(int k=0;k<=x-1;k++){ 
47.                 i+=location[k]; 
48.                } 
49.                for(;i<list.size();i++){ 
50.                String pre=list.get(i); 
51.                int len=pre.length()>name.length()? name.length():pre.length(); 
52.                for(int j=0;j<len;j++){ 
53.                    if(pre.charAt(j)>name.charAt(j)){ 
54.                        bigger=1; 
55.                        break; 
56.                    } 
57.                    else if(pre.charAt(j)<name.charAt(j)){ 
58.                        bigger=0; 
59.                        break; 
60.                    } 
61.                    else{ 
62.                        bigger=2; 
63.                    } 
64.                } 
65.                if(bigger==1||(bigger==2&&(pre.length()>name.length()))){ 
66.                    location[name.charAt(0)-'A']++; 
67.                    list.add(i,name); 
68.                    insert=1; 
69.                    break; 
70.                } 
71.            } 
72.            if(insert==0)list.add(name); 
73. 
74.        }*/  
75.        int sum=0;  
76.        int a='A'-1;  
77.        for(int i=0;i<names.length;i++){  
78.            String name=names[i];  
79.            int base=0;  
80.            for(int j=0;j<name.length();j++){  
81.                     base+=name.charAt(j)-a;  
82.            }  
83.             sum+=base*(i+1);  
84.        }  
85.        System.out.println(sum);  
86.        long end=System.currentTimeMillis();  
87.        System.out.println(end-start);  
88.  
89.    }  
90.  
91.}  


目录
相关文章
|
7月前
|
数据挖掘
|
Go C++
P1001 A+B Problem
P1001 A+B Problem
115 0
|
算法框架/工具