topcoder HowEasy

来源:互联网 发布:拷贝构造函数 java 编辑:程序博客网 时间:2024/05/29 18:22
Problem Statement    ***Note: Please keep programs under 7000 characters in length. Thank youClass Name: HowEasyMethod Name: pointValParameters: StringReturns: int TopCoder has decided to automate the process of assigning problem difficultylevels to problems. TopCoder developers have concluded that problem difficultyis 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 250point problem.If the Average Word Length is equal to 4 or 5, the problem is a 500 pointproblem.If the Average Word Length is greater than or equal to 6, the problem is a 1000point problem. Definitions:Token - a set of characters bound on either side by spaces, the beginning ofthe 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 asingle 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 problemstatement divided by the number of Words in the problem statement. Thedivision is integer division. If the number of Words is 0, the Average WordLength is 0. Implement a class HowEasy, which contains a method pointVal. The method takesa String as a parameter that is the problem statement and returns an int thatis the point value of the problem (250, 500, or 1000). The problem statementshould 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 is23/5=4, so the method should return 500.If problemStatement="523hi.", there are no Words, so the Average Word Length is0, and the method should return 250.If problemStatement="Implement a class H5 which contains some method." theAverage 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:HowEasyMethod:pointValParameters:stringReturns:intMethod signature:int pointVal(string param0)(be sure your method is public)    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.做这题的感触是,另外这题的边界条件要考虑清楚,对字符串操作不是很熟悉
#include<string>using namespace std;class HowEasy{public:int pointVal(string s){int n=0;int length=0;int j=0;for(int i=0;i<=s.size()-1;i++){if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){j++;//判断字母后面接的是否是空格或者已经到字符串末尾了if(s[i+1]==' '||i==s.size()-1){i++;n++;length+=j;j=0;}}/*若碰到字符.,如果第一个字符为.不符合规范否则判断下一个字符是否为空格或者到字符串尾端*/else if(j!=0&&s[i]=='.'&&(i==s.size()-1||s[i+1]==' ')){i++;n++;length+=j;j=0;}/*都不符合时往后查找空格,跳到空格后的字符,若超出字符串范围则结束*/else{//从当前位置往后找空格字符int pos=s.find(' ',i);if(pos>=i)i=pos;else break;j=0;}}int avg=(n!=0?length/n:0);if(avg<=3)return 250;else if(avg>=4&&avg<=5)return 500;elsereturn 1000;}};

原创粉丝点击