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);
}
}
分享到:
相关推荐
类似于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 ...
22. 修改表结构:ALTER TABLE 语句(Problem 22) ALTER TABLE 语句用于修改表的结构,例如添加或删除列。 23. 全文本搜索:MATCH() 函数(Problem 23) MATCH() 函数用于指定被搜索的列,在全文本搜索中使用。 ...
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`类展示了如何设计...
22 根据题目描述,我们可以采用数据结构如并查集或者二维四向链接来处理矩形的合并与周长计算。首先,我们需要记录每个矩形的信息,然后根据矩形的覆盖情况更新黑色区域的周长。每次增加一个矩形时,需要检查它是否...
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
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" 指的是“八中OJ(Online Judge)问题集”,这是一个在线编程竞赛平台的题目合集。八中OJ可能是某所中学的在线编程训练系统,它提供了大量的编程题目供用户练习和挑战,以提升编程能力和算法...
a) 首先掷两个骰子到3号牢房,然后爬到22号牢房 b) 然后掷6到28。 c) 最终通过2达到30。 还有其他解决方案,如(2,2,6),(2,4,4),(2,3,5)。。等 其思想是将给定的蛇梯板视为顶点数等于板中单元数...
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)的算法与源代码 电话数字键盘问题 提供移动数字键盘。您只能按向上、向左、向右或向下至当前按钮的按钮。不允许您按最下面一行的角点按钮(即.*和#)。 ...
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...
官方离线安装包,亲测可用
官方离线安装包,亲测可用
AutoCAD 2016 A Problem Solving Approach Basic and Intermediate 22nd Edition
Delphi VCLSkin 5.30 ... VCLSkin is a component to create skinnable user interface for Delphi/C++Builder application, It is easy to use, just put one component on mainform, ...* fix caption paint problem...
具有设置成本的 2D 切割库存问题数学题信息语Java [8] 集成开发环境NetBeans [8] 模块化的是的快速码信息基本使用(禁忌方法) 模式定义: PatternKind pk = new PatternKind ( 40 , ... h = 22 ; demand = 1000 }pk .
《2021美国大学生数学建模竞赛赛题解析与探讨》 美国大学生数学建模竞赛(MCM/ICM)是一项国际性的数学竞赛,旨在挑战参赛者在限定时间内,运用数学模型解决实际问题的能力。2021年的比赛主题为“2021_MCM-ICM_美国...