`
水木清华77
  • 浏览: 36261 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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);

    }

}

分享到:
评论

相关推荐

    单峰测试函数:Axis、Quadric、Rosenbrock、SchwefelProblem12、SchwefelProblem22、SumSquar、Step

    类似于Schwefel's Problem 12,Schwefel's Problem 22也是Schwefel函数系列的一部分,用于评估优化算法在处理复杂多峰函数时的表现。它的最小值可能分布在不同的区域,挑战算法的全局探索能力。 6. **SumSquar(和...

    计算机网络第六版答案

    22. Five generic tasks are error control, flow control, segmentation and reassembly, multiplexing, and connection setup. Yes, these tasks can be duplicated at different layers. For example, error ...

    MySQL数据库考试试题.docx

    22. 修改表结构:ALTER TABLE 语句(Problem 22) ALTER TABLE 语句用于修改表的结构,例如添加或删除列。 23. 全文本搜索:MATCH() 函数(Problem 23) MATCH() 函数用于指定被搜索的列,在全文本搜索中使用。 ...

    Computer-Based.Problem.Solving.Process

    Title: Computer-Based Problem Solving Process Author: Teodor Rus Length: 350 pages Edition: 1 Language: English Publisher: World ...Chapter 22. Convenience of the BOS Chapter 23. Real-Time Systems

    模糊数学作业(本章题目大部分涉及到模糊矩阵的运算)

    在第28题中,学生需要编写程序处理模糊矩阵,并将其作为输入传递给之前实现的程序,如`problem18.cpp`或`problem22.cpp`,以完成模糊矩阵的进一步处理。 最后,`fuzzy_matrix.h`中的`fuzzy_matrix`类展示了如何设计...

    Problem - 1001.pdf

    22 根据题目描述,我们可以采用数据结构如并查集或者二维四向链接来处理矩形的合并与周长计算。首先,我们需要记录每个矩形的信息,然后根据矩形的覆盖情况更新黑色区域的周长。每次增加一个矩形时,需要检查它是否...

    0-1-knapsack-problem-master (22)c.zip

    压缩包中的 "0-1-knapsack-problem-master (21)c.zip" 文件可能是误写或者重复,正常情况下应该只有一个 "0-1-knapsack-problem-master (22)c.zip" 文件,其中包含源代码、测试数据、编译脚本等资源。解压后,我们...

    Problem Solving in Data Structures & Algorithms Using Java

    Title: Problem Solving in Data Structures & Algorithms Using Java: The Ultimate Guide to Programming Author: Hemant Jain Length: 436 pages ...CHAPTER 22: INTERVIEW STRATEGY CHAPTER 23: SYSTEM DESIGN

    0-1-knapsack-problem-master (22).zip

    0-1 背包问题(0-1 Knapsack Problem)是计算机科学中的一个经典优化问题,尤其在算法竞赛和网络安全的CTF(Capture The Flag)比赛中常常出现。在这个问题中,我们有一组物品,每种物品都有一个重量和一个价值,...

    Problem.Solving.in.Data.Structures.and.Algorithms.Using.Cplusplus.epub

    Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is the skill which tech companies like Google, Amazon, Microsoft, Adobe and many others ...

    bzoj problem

    【标题】"bzoj problem" 指的是“八中OJ(Online Judge)问题集”,这是一个在线编程竞赛平台的题目合集。八中OJ可能是某所中学的在线编程训练系统,它提供了大量的编程题目供用户练习和挑战,以提升编程能力和算法...

    C#,蛇梯问题(Snake and Ladder Problem)的算法与源代码

    a) 首先掷两个骰子到3号牢房,然后爬到22号牢房 b) 然后掷6到28。 c) 最终通过2达到30。 还有其他解决方案,如(2,2,6),(2,4,4),(2,3,5)。。等 其思想是将给定的蛇梯板视为顶点数等于板中单元数...

    SMS22.ActiveX.v1.4.0.8

    SMS is the best alert to notify system managers about a problem that requires manual interference. The alerted person receives the message with a pre-programmed text and a time stamp. He can be ...

    C#,电话数字键盘问题(Mobile Numeric Keypad problem)的算法与源代码

    C#,电话数字键盘问题(Mobile Numeric Keypad problem)的算法与源代码 电话数字键盘问题 提供移动数字键盘。您只能按向上、向左、向右或向下至当前按钮的按钮。不允许您按最下面一行的角点按钮(即.*和#)。 ...

    爱立信CSR数据采集规范

    22. IOG related problem 31 22.1 Common part 31 22.2 NODE BLOCKED 33 22.3 STS Related Problems 33 22.4 Hanging System Backup 34 22.5 Charging Data Problem 35 22.6 Alarm Function Related Problem 35 22.7...

    libcurl-minimal-7.61.1-22.el8.x86_64.rpm

    官方离线安装包,亲测可用

    sane-backends-libs-1.0.27-22.el8.aarch64.rpm

    官方离线安装包,亲测可用

    0-1-knapsack-problem-master (23).zip

    【文件名称列表】:虽然提供的列表中有一个看似重复的文件名“0-1-knapsack-problem-master (22).zip”,但可以推测这可能是解压缩后的文件夹名或者是前一个版本的压缩包。通常,这样的命名可能表示这是一个项目或者...

    AutoCAD2016AProblemSolvingApproachBasicandIntermediate22ndEdition.pdf 英文原版

    AutoCAD 2016 A Problem Solving Approach Basic and Intermediate 22nd Edition

Global site tag (gtag.js) - Google Analytics