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竞赛的算法讲座ppt
Topcoder软件比赛注册方法和平台使用 Topcoder算法大赛客户端安装流程 Topcoder算法大赛客户端登陆及使用 Topcoder算法大赛注册流程 Topcoder图形比赛注册方法和平台使用
适合topcoder新手
TopCoder比赛登录使用的客户端,需要配置Java环境
TopCoder新手入门指南,一步步操作既可以了,然后开启您的Topcoder编程之旅吧。
topcoder arena,包含ContestAppletProd.jnlp,CodeProcessor.jar,FileEdit.jar,TZTester,运行需要jre环境
你可以通过这道题去了解Topcoder的题目以及比赛形式
Topcoder的Java客户端,安装前确定已经安装了JRE
用于topcoder的第3方编辑器插件。
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的比赛作品,编译通过的。可以放心使用。
给新手提供的TopCoder注册方法和平台使用 十分详细
topcoder入门,对想做tc,但又不知道怎么搞的很有帮助,我首先也不知道搞。
这是一类关于acm学习的资料,它详细的说明了Acm学习的内容,如何提高编写软件的能力
topcoder的在线答题系统
关于TopCoder的竞赛指导,不仅仅是SRM,还有Bug Race、软件比赛的资料,是我从网上收集的,大部分是中文的
关于TopCoder的一些程序竞赛的相关资料
无描述,下载的请自重 无描述,下载的请自重 无描述,下载的请自重 无描述,下载的请自重
topcoder算法讲座ppt