Problem Statement: TopCoder has decided to automate the process of assigning problem difficulty levels to problems. TopCoder developers have concluded that problem difficulty is related only to the Average Word Length of Words in the problem statement: If the Average Word Length is less than or equal to 3, the problem is a 250 point problem. If the Average Word Length is equal to 4 or 5, the problem is a 500 point problem. If the Average Word Length is greater than or equal to 6, the problem is a 1000 point problem. Definitions: Token - a set of characters bound on either side by spaces, the beginning of the input String parameter or the end of the input String parameter. Word - a Token that contains only letters (a-z or A-Z) and may end with a single period. A Word must have at least one letter. Word Length - the number of letters in a Word. (NOTE: a period is NOT a letter) The following are Words : "ab", "ab." The following are not Words : "ab..", "a.b", ".ab", "a.b.", "a2b.", "." Average Word Length - the sum of the Word Lengths of every Word in the problem statement divided by the number of Words in the problem statement. The division is integer division. If the number of Words is 0, the Average Word Length is 0. Implement a class HowEasy, which contains a method pointVal. The method takes a String as a parameter that is the problem statement and returns an int that is the point value of the problem (250, 500, or 1000). The problem statement should be processed from left to right. Here is the method signature (be sure your method is public): int pointVal(String problemStatement); problemStatement is a String containing between 1 and 50 letters, numbers, spaces, or periods. TopCoder will ensure the input is valid. Examples: If problemStatement="This is a problem statement", the Average Word Length is 23/5=4, so the method should return 500. If problemStatement="523hi.", there are no Words, so the Average Word Length is 0, and the method should return 250. If problemStatement="Implement a class H5 which contains some method." the Average Word Length is 38/7=5 and the method should return 500. If problemStatement=" no9 . wor7ds he8re. hj.." the Average Word Length is 0, and the method should return 250. |
分析:关键在于检测word,另外token是通过splite(" ")来得到也可以算做一个技巧。
解答亮点在于正则表达式的应用(比较初级,正则的强大还需要慢慢发现)
源代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author 生如夏花
*/
public class HowEasy {
/**
* @param args the command line arguments
*/
public static int pointVal(String problemStatement) {
// TODO code application logic here
String[] token;
token = problemStatement.split(" ");
int level = 250;
int num = 0;
int length = 0;
int avlength = 0;
for (String word : token) {
if (isWord(word)) {
num++;
length += word.length();
}
}
avlength = length / num;
if (avlength == 4 || avlength == 5) {
level = 500;
}
if (avlength >= 6) {
level = 1000;
}
return level;
}
public static boolean isWord(String str) {
Pattern p = Pattern.compile("[a-zA-Z]+");
Matcher m = p.matcher(str);
return m.matches();
}
}
相关推荐
【TOPCODER算法PPT1】是一份关于2007年TopCoder竞赛算法讲座的机密文件,它揭示了这个全球知名的编程竞赛平台的核心特点和价值。TopCoder社区是其核心,拥有遍布全球的会员,包括众多活跃的参赛者,涵盖了学生和专业...
TopCoder的比赛安排和练习机会可以在Active Contests和Practice Rooms中找到。Active Contests部分会列出当前正在进行的竞赛,而Practice Rooms则提供了一个练习环境,让新用户可以熟悉比赛环境,并通过解决历史题目...
1. 题库:历年比赛的题目和解题思路,帮助学习者熟悉Topcoder的赛制和题型。 2. 教程:关于算法、数据结构、编程语言的基础教程,帮助初学者建立坚实的理论基础。 3. 实战指南:针对Topcoder比赛的策略指导,如如何...
Topcoder软件比赛注册方法和平台使用 Topcoder算法大赛客户端安装流程 Topcoder算法大赛客户端登陆及使用 Topcoder算法大赛注册流程 Topcoder图形比赛注册方法和平台使用
1. **ContestAppletProd.jnlp**:这是一个Java网络启动(JNLP)文件,用于启动topcoder arena应用程序。JNLP文件是Java Web Start的一部分,它允许用户无需安装即可运行基于Java的应用程序。ContestAppletProd.jnlp...
1. **访问TopCoder网站** 首先,你需要通过浏览器访问TopCoder的官方网站:[www.topcoder.com](http://www.topcoder.com)。这是一个专门为软件开发者设计的社区,提供算法竞赛、设计竞赛和开发服务。 2. **创建...
适合topcoder新手
TopCoder比赛登录使用的客户端,需要配置Java环境
【TopCoder中文指导】是一份综合性的资源集合,旨在帮助用户了解并参与TopCoder平台的各种竞赛活动。TopCoder是一个全球知名的在线编程竞赛平台,它提供了多种类型的比赛,包括SRM(Single Round Matches)算法竞赛...
"Topcoder SRM 499 第一题详解" Topcoder SRM 499 的第一题是一道简单的 Addition Game 题目,旨在考察程序员对问题的理解和算法设计能力。本文将详细讲解该题目的知识点和解题思路。 题目分析 该题目中,Fox ...
1. **Registration.doc**:这可能是一个注册指南,详细解释如何在TopCoder平台上创建账户,参与竞赛,以及如何管理个人信息和比赛报名流程。 2. **TopCoder广东社区第一次竞赛华工赛区竞赛指南.doc**:这份文档可能...
Topcoder的Java客户端,安装前确定已经安装了JRE
1. "point-500.txt":这个名字可能暗示了一个比赛题目,其中“point-500”可能代表该问题在TopCoder平台上可能的分数。在TopCoder的马拉松匹配(Marathon Match)中,问题难度和分数通常会根据解决难度进行划分。这...
用于topcoder的第3方编辑器插件。
1. **平台**:topcoder 平台是整个生态系统的核心,它提供了在线竞赛、项目竞标、论坛讨论等功能,让程序员能够参与到全球的编程挑战中。 2. **插件**:在描述中可能指的是与topcoder平台交互的工具或增强功能,如...
TopCoper SmartWordToy problem 解决方法,C++源码。 Problem Statement The toy company "I Can't Believe It Works!...Form: http://community.topcoder.com/stat?c=problem_statement&pm=3935&rd=6532
topcoder入门,对想做tc,但又不知道怎么搞的很有帮助,我首先也不知道搞。
TOPCODER算法ppt2 TOPCODER算法讲座ppt是一个关于TopCoder算法竞赛的讲座ppt,涵盖了TopCoder竞赛的概述、Component Based Methodology、组件目录、软件需求等内容。 在Component Based Methodology中,讲座强调了...