算法题2

原创 2012年03月21日 23:19:03

题目:


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 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>
#include<iostream>
using namespace std;
class HowEasy{
public:
  HowEasy(){
    sum = 0;
    number = 0;
  };
  ~HowEasy(){};
  int pointVal(string param0){
  string::iterator iter = param0.begin();
  string::iterator iter2;
  getNext(iter,param0.end());
  bool _valid = true;
  int length = 0;
  while(iter < param0.end()){
   iter2 = getNextSpace(iter,_valid,length,param0.end());
    if(_valid){
      number++;
      sum += length;
    
    }
      iter = iter2;
      getNext(iter,param0.end());
  
  }
  int average = sum / number;
  if(average < 4){
    return 250; 
  }
  else if(average < 6){
    return 500;
  }
  else
    return 1000;
  
  };
  
private:
  bool isLetter(char x){
    return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
  };
 void getNext(string::iterator & iter,string::iterator end){
    while(iter < end && *iter == ' ')
      iter++;
  };
  string::iterator getNextSpace(string::iterator iter,bool & valid,int & length,string::iterator end){
    length = 0;
    valid = true;
    while(iter < end && *iter != ' ' ){
      if(!isLetter(*iter)){
        
        if(valid == true && (*iter) == '.' && iter + 1 < end && *(iter+1) == ' '){
          valid = true;
        }
        else{
          valid = false;
        }
      }
      length++;
      iter++;
    }
    return iter;
  };
  int number;
  int sum;
 
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?

合并两个二叉树。规则是,对应的两个结点如果有重叠就求和(比如更改val),如果不重叠就使用非空的结点的值。(不考虑树释放的问题。) 演示递归的使用,如何切入与结束,如何保持清醒

Java基础算法练习五题——(2)

6.输入两个正整数m和n,求其最大公约数和最小公倍数。 package com.myd.ex2; import java.util.*; public class Zzxc { public...

【微软100题】一个台阶总共有n 级,如果一次可以跳1 级,也可以跳2 级,求总共有多少总跳法,并分析算法的时间复杂度

一个台阶总共有n 级,如果一次可以跳1 级,也可以跳2 级,求总共有多少总跳法,并分析算法的时间复杂度

递归算法题2

第一个递归算法题: 就是把一个二维数组,通过循环,先横向循环,再纵向循环,再向左循环,再向上循环,最后运行结果如下: 描述如下:  //0,0,0,0,0  //0,0,0,0,0  //0,0,0,...

机试算法题(2)--成绩排序

1.题目描述:     有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。 输入:    ...

[刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence

书上具体所有题目:http://pan.baidu.com/s/1hssH0KO代码:(Accepted,20 ms)//UVa1594 - Ducci Sequence #include #incl...

Codeforces 617E XOR and Favorite Number (Round #340 (Div. 2) E题) 莫队算法 + 异或基本性质

题意 给你一个数组和一个数k,然后有m个查询,每次给你l,r,问你[l , r]区间内有多少对(i,j),使得a[i] ^ a[i+1] … ^ a[j] = k 思路 pref[i]记录a[0] ^...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)