算法题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;
 
};


leetcode2题 题解 翻译 C语言版 Python版

2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi...
  • yingzinanfei
  • yingzinanfei
  • 2016年11月08日 22:51
  • 909

字符串方面的几道简单算法题

字符串问题,也是笔试面试的重头戏,可以考察很多方面,比如思路,比如基础的算法。温故而知新,今天整理了几道有关字符串的简单算法题,解法如下代码所示。不对之处,欢迎大家批评指正。 package Ch...
  • u010589037
  • u010589037
  • 2016年07月31日 17:55
  • 1136

每天刷个算法题20160522:支持各种类型的并查集

为了防止思维僵化,每天刷个算法题。这里是支持各种类型的并查集。
  • u012077163
  • u012077163
  • 2016年05月28日 14:09
  • 2674

2014各大网络公司校招笔试算法题(收集并更新中)

从博客中整理,并不断的更新,供大家学习和交流,随后会给出部分算法题的参考代码。 腾讯   1、请设计一个排队系统,能够让每个进入队伍的用户都能看到自己在 中所处的位置和变化。...
  • u012138828
  • u012138828
  • 2014年08月25日 13:35
  • 1250

趣味Java算法题(附答案)

【程序1】    题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?    //这是一个菲...
  • zyqyz520
  • zyqyz520
  • 2014年07月02日 23:53
  • 2434

算法训练 最大最小公倍数 简单贪心算法

算法训练 最大最小公倍数   时间限制:1.0s   内存限制:256.0MB 问题描述 已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。...
  • wr132
  • wr132
  • 2015年02月05日 21:56
  • 3559

java一些经典算法题

·1 *求1!+2!+3!.....+30!的和 并用方法。 int a=1,b=30; qiuhe(a, b);*/ public class d501 { public static vo...
  • qq625315759
  • qq625315759
  • 2016年07月09日 17:02
  • 333

【常用算法思路分析系列】排序高频题集

最近在牛客上整理常用的一些算法思路,【常用算法思路分析系列】主要是针对一些高频算法笔试、面试题目的解题思路进行总结,大部分也给出了具体的代码实现,本篇文章是对排序相关题目的思路分析。(1)对一个基本有...
  • shakespeare001
  • shakespeare001
  • 2016年05月16日 07:56
  • 5134

2015阿里秋招其中一个算法题(经典)

写一个函数,输入一个二叉树,树中每个节点存放了一个整数值,函数返回这棵二叉树中相差最大的两个节点间的差值绝对值。请注意程序效率 这是2015阿里秋招的一个在线笔试题 实现方法很简单,遍历一遍...
  • u014082714
  • u014082714
  • 2015年03月15日 12:34
  • 1409

经典算法题一览

http://www.cnblogs.com/wei-li/p/3318929.html http://www.cnblogs.com/wei-li/p/3318929.html ...
  • sxb0841901116
  • sxb0841901116
  • 2015年04月06日 21:31
  • 1965
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:算法题2
举报原因:
原因补充:

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