Description
Snooker is a cue sport played by two players on a rectangular table. The players take turns topot a series of balls of varying colours, where each colour represents a distinct point value forpotting the ball.
A player may pot any ball on the table initially, however any subsequent shots must follow apattern: if the previous ball was red, the next ball must be another colour; otherwise, if there arestill red balls left, the next ball must be red.
Balls of any colour other than red are initially replaced on the table every time they are potted,and may be used again to score more points. The balls stop being replaced once all of the redballs have been potted.
The values of each coloured ball are:
Colour red yellow green brown blue pink black
Value 1 2 3 4 5 6 7
Snooker players are respected universally for their prowess in mental arithmetic. One sweepingglance across the table is enough to tell an experienced contestant how much they could score.For newer players, however, this is a challenge. Write a program to help calculate a score for agiven list of balls remaining on the table.
Input
• one line containing the integers N (1 ≤ N ≤ 21), the number of balls remaining on thetable
.• N further lines, each containing the colour of one of the balls on the table.
The list of balls will not be ordered in any way and will contain at most one of each of yellow,green, brown, blue, pink and black.
Output
Output the largest possible score the player can make.
Sample Input
5
red
black
pink
red
red
Sample Output
37
Sample Input
3blue
black
pink
Sample Output
18
Sample Input
8
yellow
green
brown
red
red
red
red
red
Sample Output
34
Sample Input
2
red
red
Sample Output
1
题解:每当打完一个红球,必须打一个彩球,否则结束。打完红球打彩球,彩球加分后仍会出现在桌面上。
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
int a[8]={0},t,s=0;
cin>>t;
int tt=t;
while(t--)
{
char b[10];
cin>>b;
if(strcmp(b,"red")==0)
s++;
if(strcmp(b,"yellow")==0)
a[2]++;
if(strcmp(b,"green")==0)
a[3]++;
if(strcmp(b,"brown")==0)
a[4]++;
if(strcmp(b,"blue")==0)
a[5]++;
if(strcmp(b,"pink")==0)
a[6]++;
if(strcmp(b,"black")==0)
a[7]++;
}
int max=1,sum=0;
for(int i=2;i<=7;i++)
{
sum+=i*a[i];
if(a[i]!=0&&max<i)
max=i;
}
sum+=s*max+s;
if(s==tt)
cout<<"1"<<endl;
else
cout<<sum<<endl;
return 0;
}