问题描述:
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
解决问题:
这个没什么难度。
我们先找到对应关系。
使用数组就够了。
public class Problem42 {
public static int Len = 0;
public static String[] readNames(){
List<String> names = new ArrayList<String>();
StringBuffer text = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader("f:/words.txt"));
String line ;
while((line=br.readLine())!=null){
text.append(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String regex = "\"(.*?)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text.toString());
System.out.println("Number:"+matcher.groupCount());
long total = 0;
int index = 0;
while(matcher.find()){
String name = matcher.group(1);
names.add(name);
if(name.length()>Len)
Len = name.length();
}
System.out.println(names);
String[] result =(String[]) names.toArray(new String[0]);
return result;
}
public static int count_word(String word){
int total = 0;
for(int i=0; i<word.length(); i++){
total+=word.charAt(i)-'A'+1;
}
return total;
}
public static void main(String[] args){
String[] words = readNames();
int sum = 0;
int MAX = Len*26+1;
boolean[] numbers = new boolean[MAX];
Arrays.fill(numbers, false);
int index =1;
int triangle ;
System.out.println("Len:"+numbers.length);
do{
triangle = (index*(index+1))/2;
System.out.println("triangle:"+triangle);
numbers[triangle] = true;
index++;
}while(triangle <MAX);
for(int i=0; i<words.length; i++){
int value = count_word(words[i]);
if(numbers[value]){
sum++;
}
}
System.out.println(sum);
}
}
分享到:
相关推荐
- **算法编写**: 第42页提到为项目5编写算法,可能是在实际编程练习中如何设计解决问题的步骤。 - **算术表达式**: 第72页涉及算术表达式的编写和声明,这是编程中最基础的技能之一。 - **字符串处理**: 第87页涉及...
通过监测身体上的42个点,可以得到包括重心位置、运动轨迹在内的25个身体平衡特征,如重心的前后左右移动范围、步态的稳定性及各关节活动度。通过建立特征提取模型,我们可以量化这些特性,进而全面评估老年人的平衡...
Problem42 of LeetCode 题目:给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水 这道题关键在于转换看问题的视角 不要把问题当成水来看,而是当成光来看,能够被从两侧...
旅行商问题,(Traveling Saleman Problem,TSP)是VRP的特例,由于Gaery[1]已证明TSP问题是NP难题,因此,VRP也属于NP难题。旅行商问题(TSP)又译为旅行推销员问题、货郎担问题,简称为TSP问题,是最基本的路线...
The aim of this study was to analyze the WISC-R profiles of 42 conduct problem children attending public school classes for the emotionally handicapped. This analysis was done to generate hypotheses...
流水车间调度问题(Job Shop Scheduling Problem,JSSP)是工业工程和运筹学领域中的一个重要问题,它涉及到如何优化生产流程,以最小化加工时间或完成所有任务的总延迟时间。在这个特定的算例中,我们关注的是与...
8D(8 Disciplines Problem Solving)是一种结构化的问题解决方法,主要应用于质量管理和持续改进领域,由Ford公司在20世纪80年代发展起来。8D程序是一种系统性、团队导向的解决问题的方法,旨在识别问题的根本原因...
回文问题 F(n) = b: n是一个正整数 b是最小整数基数 >= 2 使得n可以表示为 例子: F(0) = 2: b=2: (0 base 10) = 0*(2^0) = (0 base 2) ...b=4: (42 base 10) = 2*(4^2) + 2*(4^1) + 2*(4^0) = (222 base 4)
Coarse-grained ...approach can improve the performance of the kernels by 42% on average, as compared with the state-of-the-art methods. The runtime complexity of our approach is also acceptable.
The fusion between graph theory and ...CHAPTER 42 - Approximation Algorithms for Connectivity Problems CHAPTER 43 - Rectilinear Steiner Minimum Trees CHAPTER 44 - Parameter Algorithms and Complexity
Elmyr > Elmyr is a Kotlin library providing tools to generate “random” values, ...The problem is, the more test I write, the less I'm confident in my fake values, because they're always the same.
15.4 PROBLEM THREE: MODIFY EVERY RECORD OF A FASTA FILE 319 15.4.1 Commented Source Code 320 Chapter 16 Web Application for Filtering Vector Contamination 321 16.1 PROBLEM DESCRIPTION 321 16.1.1 ...
背包问题动态规划背包问题是组合优化中的一个问题: 给定一... 例子: 20 5 安妮 15 1 毕加索 10 9 波波8 3 罗纳尔多 5 4 血管 9 6 此示例的输出为 42,这是 20 公斤容量中的最佳数量 os 值。 您可以在这里测试项目: :
leetcode双人赛 Online-Judge 目录说明 utils:小工具包 Readme.py: A script that generates the README document. Spider.py: A spider ...problem ...problem ...problem ...42 NoneCpp 43 NoneCpp 44 NoneCp
Computer Networking: A Top-Down Approach, 6th Edition Solutions to Review Questions and Problems Version Date: May 2012 ...This document contains the solutions to review questions ...Problem 1 There...
##### 42. **LDAP_AUTH_UNKNOWN (86)** **描述:** 未知的认证方法。 **处理方式:** 确认认证方法是否被LDAP服务器支持。 ##### 43. **LDAP_FILTER_ERROR (87)** **描述:** 过滤器错误。 **处理方式:** 检查...
6. **快速解决问题(Rapid Problem Solving)**:采用结构化的D0到D8步骤,包括识别问题、组建团队、遏制症状、分析根本原因、选择纠正措施、验证有效性、预防问题再次发生和关闭问题解决过程。5W+1H和5Why工具帮助...
C1.5 虚方法和抽象类 42 C1.5.1 虚方法 42 C1.5.2 抽象类 43 第2章 递归:镜子 45 2.1 递归解决方案 46 2.2 返回值的递归 48 2.2.1 递归值函数:n的阶乘 49 2.2.2 箱式跟踪 52 2.3 执行动作的递归 55 2.4 ...
42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone Advanced Mail PlugIn Delete V1.1.0 For Adobe Dreamweaver 44. DwZone Advanced Mail V2.2.3 For Adobe Dreamweaver 45. DwZone ...