免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java Problem22
最近访问板块 发新帖
查看: 1550 | 回复: 1
打印 上一主题 下一主题

Problem22 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 20:30 |只看该作者 |倒序浏览

Problem22











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

论坛徽章:
0
2 [报告]
发表于 2012-03-19 20:30 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP