C实现HowEasy

 

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)

    

This problem statement is the exclusive and proprietary property ofTopCoder, Inc. Any unauthorized use or reproduction of this information withoutthe prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003,TopCoder, Inc. All rights reserved.

 

//代码部分

 

#include <stdio.h>
#include <string.h>
#include <cstdlib>

#define N 50

int main()
{
  int n=0;
  int length=0;
  int i=0,j=0;
  int avg=0;
  int flag=0;
 
  char str[N];
 
  puts("输入字符:");
  gets(str);
 
  for(i=0;i<strlen(str)-1;i++)
    {
     if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
       {
        j++;
        //判断后面空格或者结尾
        if(str[i+1]==' ' || i==strlen(str)-1)
          {
           i++;
           n++;
           length+=j;
           j=0;   
          }  
       }
       //遇到字符.,判断是否合法,否则判断下一个字符是空格或者结束
       else if(j!=0 && str[i]=='.' && (i==strlen(str)-1 || str[i+1]==' '))
         {
          i++;
          n++;
          length+=j;
          j=0;   
         }
      //都不符合,往后查找,跳到空格后的字符
     
      else
        {
         for(i=0;i<strlen(str)-1;i++)
           {
            if(str[i]==' ')
            flag=i;   
            }
          if(flag>=i)
            i=flag;
          else
             break;
           j=0;
        }  
    }
   
   avg=(n!=0?length/n:0); 
   if(avg<=3) 
     //return 250;
     puts("250");
   else if(avg>=4&&avg<=5) 
   //return 500;
     puts("500"); 
   else 
     //return 1000;
     puts("1000");         
  
  system("pause");  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值