Archive for the ‘TopCoder’ Category

算法第二题

星期二, 十月 9th, 2007

这次比较顺利,1个小时,呵呵,虽然时间仍然算长,不过很长时间没摸过了已经。

题目:
Problem Statement
????
***Note:  Please keep programs under 7000 characters in length.  Thank you
Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int

Define the function S(x) as the sum of the squares of the digits of x. �
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.

Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x.  That is: S(x), S(S(x)), S(S(S(x))), etc…
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.�
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x. 

Implement a class SquareDigits, which contains a method smallestResult.  The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.

The method signature is (be sure your method is public):
int smallestResult(int n);

TopCoder will ensure n is non-negative and is between 0 and 199 inclusive.

Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.

If n=2: T(0) through T(10) do not contain the value 2.  If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.

If n=10: T(0) through T(6) do not contain 10.  If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat…
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.

n=1 -> x=1
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition
????
Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int 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<iostream>
using namespace std;
const int MAXLENGTH = 1000;
class SquareDigits
{
public:
 SquareDigits()
 {
  sresult = 0;
  smallResult = 0;
  ti = 0;
  lengthoftresult = 0;
  xinput = 0;
  number = 0;
  for(int i=0;i<MAXLENGTH; i++)
   tresult[i]=”;
 }
 int s(int x)
 {
  if(x<10)
   sresult += x*x;
  else
  {
   int mod = x%10;
   sresult += mod*mod;
   s(x/10);
  }
  return sresult;
 }
 int* t(int x)
 {
  xinput = x;
  lengthoftresult = 0;
  return t1(x);
 }

 int* t1(int x)
 {
  sresult = 0;
  if(x == 0 || x==1)
  {
   tresult[ti]=x;
   lengthoftresult++;
  }
  else
  {
   int temp = s(x);
   if(xinput != temp && ti<MAXLENGTH)
   {
    for(int i = 0; i<lengthoftresult-1;i++)
    {
     if(temp == tresult[i])
      return tresult;
    }
    tresult[ti] = temp;
    ti++;
    lengthoftresult++;
    t1(temp);
   }
   ti = 0;
  }
  return tresult;
 }

 int smallestResult(int n)
 {
  number = n;
  int i=0;
  do
  {
   t(i);
   i++;
  }while(comparetsmall()!=true);
  smallResult = i -1 ;
  return smallResult;
 }
 bool comparetsmall()
 {
  for(int i=0; i<lengthoftresult; i++)
  {
  �
   if(tresult[i] == number)
    return true;
  }
  return false;
 }
private:
 int sresult;
 int tresult[MAXLENGTH];
 int xinput;
 int lengthoftresult;
 int ti;
 int smallResult;
 int number;
};

void main()
{
 SquareDigits sd;

 int x = 0;
 cout<<”Input the param of n:”<<endl;
 cin>>x;
 //cout<<”x is:”<<x<<endl;

 int smallresult = sd.smallestResult(x);
 cout<<”smallestresult is:”<<smallresult<<endl;

 //sd.t(x);
}

在TopCoder上算法类的第一题

星期一, 十月 8th, 2007

题目:

***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.

其实很简单,就是让你提供一个类,然后具体功能看描述。

我巨汗,竟然折腾了1个半小时才弄出来,这题250分,我就得了76.9分,可怜,下面是解法:

#include<iostream>
#include<string>
using namespace std;

class HowEasy
{
public:
 int pointVal(string str)
 {
  int point = 0;
  int awlength = 0;
  int words = 0;
  int letters = 0;
  char end = ”;
  bool flag = false;
  basic_string <char>::iterator str_Iter;
  for ( str_Iter = str.begin( ); str_Iter != str.end( ); str_Iter++ )
  {
   char temp = *str_Iter;
   if ((temp<= ‘z’&&temp>=’a')||(temp>=’A'&&temp<=’Z'))
   {
    letters++;
    flag = true;
   }
   else
   {
    flag = false;
   }

   if(temp == ‘ ‘)
    words ++;

  }

  basic_string <char>::iterator str_Iter1;
  str_Iter1 = str.end();
  end = *(str_Iter1-1);
  if(end == ‘.’&& flag!= true)
   words++;
  if(flag == true && end!=’.')
   words++;
  if(words != 0)
   awlength = letters/words;

  if(awlength <= 3 && awlength >= 0)
   point = 250;
  else if(awlength == 4 || awlength == 5)
   point = 500;
  else if(awlength >= 6)
   point = 1000;
  return point;
 }

};

这是完整的测试:

// HowEasy.cpp : 定义控制台应用程序的入口点。
//

#include<iostream>
#include<string>
using namespace std;

class HowEasy
{
public:
 int pointVal(string str)
 {
  int point = 0;
  int awlength = 0;
  int words = 0;
  int letters = 0;
  char end = ”;
  bool flag = false;
  basic_string <char>::iterator str_Iter;
  for ( str_Iter = str.begin( ); str_Iter != str.end( ); str_Iter++ )
  {
   char temp = *str_Iter;
   if ((temp<= ‘z’&&temp>=’a')||(temp>=’A'&&temp<=’Z'))
   {
    letters++;
    flag = true;
   }
   else
   {
    flag = false;
   }

   if(temp == ‘ ‘)
    words ++;

  }

  basic_string <char>::iterator str_Iter1;
  str_Iter1 = str.end();
  end = *(str_Iter1-1);
  if(end == ‘.’&& flag!= true)
   words++;
  if(flag == true && end!=’.')
   words++;
  if(words != 0)
   awlength = letters/words;

  if(awlength <= 3 && awlength >= 0)
   point = 250;
  else if(awlength == 4 || awlength == 5)
   point = 500;
  else if(awlength >= 6)
   point = 1000;
  return point;
 }

};

int main()
{

 HowEasy he;
 cout<<”Please input the string:”<<endl;
 string str;
 getline(cin, str, ‘\n’);

 int length = he.pointVal(str);
}

特别不好意思的是:C++的基本语法都忘记了,也很久没有写控制台的程序了,呵呵,都回忆了一下才想起来。真的很脸红。