posts - 3, comments - 5, trackbacks - 0, articles - 3
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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)

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.

class HowEasy
{
    
private:
    
int HowEasy::nextToken(string pChar,bool &bInvalidToken,int &idx);
public:    
    HowEasy();
    
int pointVal(string param0);

}
;
HowEasy::HowEasy()
{
}
;

int HowEasy::nextToken(string pChar,bool &bInvalidToken,int &idx)
{
    
int result = 0;
    bInvalidToken 
= false;

    
while( pChar[idx] == ' ' )
        idx 
= idx+1;
        
    
while(pChar[idx] !=' ' && pChar[idx] !='\0')
    
{
        
if( ( 'a' <=pChar[idx] && pChar[idx] <='z' )
            
|| ('A' <= pChar[idx] && pChar[idx] <='Z'
            
|| ('.' == pChar[idx] && pChar[idx+1== '\0') )
        
{
        }

        
else
        
{
            result 
= 0;
            bInvalidToken 
= true;            
        }

        
if!bInvalidToken )
        
{
            result
++;
        }

        
if( pChar[idx] !='\0')
            idx
++;
    }

    
if( pChar[idx] ==' ' )
        idx
++;
    
return result;
}

int HowEasy::pointVal(string param0)
{
    
int nTotalChars = 0;
    
int nTotoalWords = 0;
    
int idx = 0;
    
bool bInvalidToken = false;
    
    
    
while( param0[idx] != '\0' )
    
{
        
        nTotalChars 
+= this->nextToken(param0,bInvalidToken,idx);
        
if!bInvalidToken )
            nTotoalWords
++;
        /*
           else
        
{
            nTotoalWords 
= 0;
            
break;
        }
  */
    }

    
    
if( nTotoalWords == 0 )
    
{
        
return 250;
    }

    
else if( nTotalChars/nTotoalWords <=3 )
    
{
        
return 250;
    }

    
else if( nTotalChars/nTotoalWords >=4 && nTotalChars/nTotoalWords <=5 )
    
{
        
return 500;
    }

    
else if( nTotalChars/nTotoalWords >= 6 )
    
{
        
return 1000;
    }

    
return 250;
}
;

Feedback

# re: Topcoder -Tournament -2001Round1 得分249.15/250  回复  更多评论   

2009-01-01 21:09 by 王清龙
大哥,我问你个问题"A. ABCDEF"这个应该是算有几个word?我觉的如果输入这个应该是输出1000,不过我测试的时候发现标准答案输出的是250,而其我看了你的codes,你是如果遇到不是一个词,就break了。
而且我试验过你的程序,用这个“Implement a class H5 which contains some method”做输入的话,标准是500但是你的是输出250.
看到这个可以与我联系
boylong@gmail.com

# re: Topcoder -Tournament -2001Round1 得分249.15/250  回复  更多评论   

2009-01-01 21:26 by 王清龙
我根据这两个字符串去challenge别人的代码,发现一个问题,就是好像java的代码没有出问题,其他的语言的代码出问题过。

# re: Topcoder -Tournament -2001Round1 得分249.15/250  回复  更多评论   

2009-01-01 21:31 by 王清龙
C#的代码好像也可以,好像用正则表达式的可以。。。。。无语。。。。

# re: Topcoder -Tournament -2001Round1 得分249.15/250[未登录]  回复  更多评论   

2009-01-03 15:49 by 赵均泽
我重新修改了下,A. ABCDEF是1个单词,6个字母,所以是1000

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理