一道NSA上看到的题,大家一起讨论下!
Problem Statement
***Note: Please keep programs under 7000 characters in length. Thank you
Class Name: HowEasy
Method Name: pointVal
Parameters: String
Returns: int
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.
Definition
Class:
HowEasy
Method:
pointVal
Parameters:
String
Returns:
int
Method signature:
int pointVal(String param0)
(be sure your method is public)
[解决办法]
能做出这道题,就不用做软件工程师了,可以做翻译了。。。
[解决办法]
这个题不难吧,topcoder里一道250分的题。用正则就行啊
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(); }}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/NiuGuy/archive/2009/06/18/4281168.aspx
[解决办法]
题目就是求一个字符串中单词的个数/所有单词的长度之和。单词的定义是只能由字母组成,可以是大写亦可以是小写,但是不能包含数字,单词的末尾可以包含".",这个"."不是字母。
[解决办法]
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(word.charAt(word.length-1)=='.') { word = word.substring(0,word.length-1); if (isWord(word)) { num++; length += word.length(); } } if(num == 0) { avlength=0; } else { avlength = length / num; } ..........................
[解决办法]
//h 文件#include <iostream>#include <vector>using namespace std;class HowEasy{public: HowEasy() { } static int pointVal(string problemString);private: /* 函数说明: split 根据分隔符拆分字符串; 函数参数: problemString 为要拆分的字符串,splitChar 为分隔符; 函数返回值: 返回拆分厚的字符串到vector 中 */ static vector<string> split(string problemString , char splitChar); /* 函数说明: isLetter 测试字符是否为字母; 函数返回值: true 代表字符是字母,否则不是 */ static bool isLetter(char testChar); /* 函数说明:isWord 测试一个字符串是否为单词; 函数返回值:true代表是一个单词 */ static bool isWord(string testString);};//cpp 文件#include "HowEasy.h"vector<string> HowEasy::split(std::string problemString, char splitChar){ vector<string> vecResult; bool isSingle = true; //字符串中是否包含分隔符 size_t beginIndex = 0; int count =0; for(size_t i =0 ;i < problemString.size(); i++) { count++; //判断字符是否为要分隔的字符 if(problemString.at(i) == splitChar) { isSingle =false; //如果count 为1 则证明只有一个分隔符不用加入vector 中了 if(count != 1) { string subString = problemString.substr(beginIndex,count-1); vecResult.push_back(subString); } beginIndex = i+1; count= 0; } } //如果是字符串中不包含空格,则将整个字符串压入Vector中,否则将最后字符串压入Vector中 if(isSingle) vecResult.push_back(problemString); else vecResult.push_back(problemString.substr(beginIndex,count)); return vecResult;}bool HowEasy::isLetter(char tempChar){ if( (tempChar >= 'A' && tempChar <= 'Z') || (tempChar >= 'a' && tempChar <= 'z')) return true; return false;}bool HowEasy::isWord(std::string testString){ size_t length = testString.size(); //处理空的字符串 if(length == 0) return false; //处理0至size-2下标的字符。因为最后一位可能为"."; for(size_t i =0 ;i < length-1;i++) { if( !isLetter(testString.at(i))) return false; } //处理字符串最后一位; if(isLetter(testString.at(length-1)) || testString.at(length -1) == '.') return true; else return false; return true;}int HowEasy::pointVal(std::string problemString){ vector<string> tempVec = split(problemString ,' '); //打印测试 for(int j =0 ;j < tempVec.size();j++) { cout << tempVec[j].c_str() << endl; } int wordCount = 0; //单词个数 size_t length =0; //单词总长度 for(size_t i = 0; i< tempVec.size() ;i++) { if(isWord(tempVec[i])) { wordCount++; //测试最后一位是否为".", 为"." 则将其长度减1,否则不减 if(tempVec[i].at(tempVec[i].size() -1) != '.') length += tempVec[i].size(); else length += tempVec[i].size()-1; } } //处理Length =0,因为如果Length =0 ,则wordCount =0不能做除法; if(length == 0) return 250; cout << "wordCount : " << wordCount << endl; cout << "length : " << length << endl; size_t score = length / wordCount; if(score <= 3) return 250; else if(score <=5 && score >= 4) { return 500; } else return 1000;}//测试CPP文件#include "HowEasy.h"int main(int argc,char* argv[]){ string testString =" Implement a class H5 which contains some method." ; int result = HowEasy::pointVal(testString); cout << "result is " << result << endl; return 0;}