SRM 148 1A2A 2013.12.2

来源:互联网 发布:神仙传2源码 编辑:程序博客网 时间:2024/05/10 00:54

SRM 148 1A2A 2013.12.2

DIV 1

250

 

Problem Statement

    

Tommy is learning a simple card game calledCircle. To play the game, the single player shuffles a deck of cards. He or shethen flips through the deck, removing all instances of the 'K' card, and allconsecutive pairs of cards that add up to 13. The deck does wrap around, sothat if the last card remaining in the deck and the first card remaining in thedeck add up to 13, they are both removed. The player keeps cycling through thedeck until no more cards can be removed.

Create a class CircleGame containing themethod cardsLeft that takes a string deck representing a not-necessarilycomplete nor correct deck of cards. Each character of deck represents the valueof a card without the suit. The values shown on the card represent thefollowing numerical values:

'A' - 1

'2' - 2

'3' - 3

'4' - 4

'5' - 5

'6' - 6

'7' - 7

'8' - 8

'9' - 9

'T' - 10

'J' - 11

'Q' - 12

'K' - 13

Given deck, return the number of cardsremaining after the game has been played to its fullest such that no more cardscan be removed.

Definition

    

Class:

CircleGame

Method:

cardsLeft

Parameters:

string

Returns:

int

Method signature:

int cardsLeft(string deck)

(be sure your method is public)

    

 

Notes

-

There are no guarantees made about thecontents of the deck. For example, there can be more than four of a given valueof card.

-

Note that if a particular card can make asum of 13 with the card before or after it, it does not matter which is chosen.For example, 38582, whether you use the first 8 or the second 8, will stillbecome 382 after the two cards (5 and 8) are removed.

Constraints

-

deck will have between 10 and 50characters, inclusive.

-

each character of deck will be a characterfrom the set {'2'-'9','A','T','J','Q','K'}.

Examples

0)

 

    

"KKKKKKKKKK"

Returns: 0

All K cards are always removed from thedeck.

1)

 

    

"KKKKKAQT23"

Returns: 1

The K cards are removed, leaving AQT23. AQare then removed because they add up to 13, leaving T23. Since the deck wrapsaround and T and 3 add up to 13, they are also removed, just leaving the 2.

2)

 

    

"KKKKATQ23J"

Returns: 6

Only the K cards can be removed.

3)

 

    

"AT68482AK6875QJ5K9573Q"

Returns: 4

The remaining deck is 6875.

4)

 

    

"AQK262362TKKAQ6262437892KTTJA332"

Returns: 24

 

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

92.97 Code

#include<string>

#include<vector>

#include<iostream>

#include<stdio.h>

 

using namespace std;

 

class CircleGame

{

public:char value[260];

 

public:CircleGame()

         {

                   value['A']=1;

                   value['2']=2;

                   value['3']=3;

                   value['4']=4;

                   value['5']=5;

                   value['6']=6;

                   value['7']=7;

                   value['8']=8;

                   value['9']=9;

                   value['T']=10;

                   value['J']=11;

                   value['Q']=12;

                   value['K']=13;           

         }

 

public:int cardsLeft(string deck)

            {

                      vector<int> num;

                      num.clear();

                      vector<int> alive;

                      alive.clear();

                      for(int i=0;i<deck.size();i++)

                      {

                               int temp=value[deck[i]];

                               if (temp!=13)

                               {

                                        num.push_back(temp);

                                        alive.push_back(1);

                               }

                      }

                      int lennum=num.size();

                      for (int x=0;x<lennum;x++)

                      {

                               num.push_back(num[x]);

                      }

                      bool flag=false;

                      int p=0,p1,p2;

                      int n=num.size();

                      int lenv=alive.size();

                      int last=lenv-1;

                      while(true)

                      { 

                               while ((alive[p%lenv]==0)&&(p<n))p++;

                               p1=p;

                               p++;

                               while ((alive[p%lenv]==0)&&(p<n))p++;

                               p2=p;

                               if ((p1!=p2)&&(num[p1]+num[p2]==13))

                               {

                                        alive[p1%lenv]=0;

                                        alive[p2%lenv]=0;

                                        flag=true;

                                        for(int f=lenv-1;f!=0;f--)

                                        {

                                                  if (alive[f]==1)

                                                  {

                                                           last=f;

                                                           break;

                                                  }

                                        }

                               }

                               p=p2%lenv;

                               if ((p1==last)&&(!flag)) break;

                               if (p1==last) flag=false;

                      }

                      int ans=0;

                      for(int k=0;k<alive.size();k++)

                      {

                               if (alive[k]==1) ans++;

                      }

                      return ans;

            }

};

 

DIV 2

250

Problem Statement

    

Create a class DivisorDigits containing amethod howMany which takes an int number and returns how many digits in numberdivide evenly into number itself.

Definition

    

Class:

DivisorDigits

Method:

howMany

Parameters:

int

Returns:

int

Method signature:

int howMany(int number)

(be sure your method is public)

    

 

Notes

-

No number is divisible by 0.

Constraints

-

number will be between 10000 and 999999999.

Examples

0)

 

    

12345

Returns: 3

12345 is divisible by 1, 3, and 5.

1)

 

    

661232

Returns: 3

661232 is divisible by 1 and 2.

2)

 

    

52527

Returns: 0

52527 is not divisible by 5, 2, or 7.

3)

 

    

730000000

Returns: 0

Nothing is divisible by 0. In this case,the number is also not divisible by 7 or 3.

This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

238.21 Code

#include<string>

#include<vector>

#include<iostream>

 

using namespace std;

 

class DivisorDigits

{

public:int howMany(int number)

         {

                      int num[15]={0};

                      int org=number;

                      int ans=0;

                      while (number!=0)

                      {

                               int temp=number%10;

                               number/=10;

                               if (temp!=0)

                               {

                                        if ((num[temp]==0)&&(org%temp==0))

                                       {

                                                  ans++;

                                                  num[temp]=1;

                                        }

                               }

 

                      }

                      return ans;

         }

};

 

0 0
原创粉丝点击