麻烦+找规律+各种情况

E - E
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:

  • take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
  • take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.

She repeats this process until there is only one card left. What are the possible colors for the final card?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.

The next line contains a string s of length n — the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.

Output

Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.

Sample Input

Input
2
RB
Output
G
Input
3
GRG
Output
BR
Input
5
BBBBB
Output
B

Hint

In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.

In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card.

In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.


        题意:有n张卡片,颜色有B,G,R三种,两张不动颜色的卡片合成一张第三种颜色的卡片,两张相同颜色的卡片合成该颜色的一张卡片。  按照此规则,任意组合,输出最后一张卡片的颜色,输出所有可能。

解题思路:

      1.当n张卡片只有一种颜色,最后一张肯定就是该颜色。  

      2.当n(n>2)张卡片里有两种颜色,A有(n-1)张,B只有1张,那么结果可以为B,C两种颜色。

      3.当n==2,且有A,B颜色卡片各一张,则最后颜色为C颜色。

      4.其他情况均有A,B,C这三种颜色。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    char str[220];
    int sum[3];
    while(scanf("%d",&n)!=EOF)
    {
        memset(str,0,sizeof(str));
        scanf("%s",str);
        if(n==1)
        {
            printf("%c\n",str[0]);
            continue;
        }
        memset(sum,0,sizeof(sum));
        for(int i=0;i<n;i++)
        {
            if(str[i]=='B')
                sum[0]++;
            else if(str[i]=='G')
                sum[1]++;
            else  
                sum[2]++;
        }
		if(sum[0]>=1&&sum[1]==0&&sum[2]==0)//BBBBB 
		{
			printf("B\n");
		}
		else if(sum[1]>=1&&sum[0]==0&&sum[2]==0)//GGGGG
		{
			printf("G\n");
		}
		else if(sum[2]>=1&&sum[1]==0&&sum[0]==0)RRRRR
		{
			printf("R\n");	
		}
		else if(sum[0]==1&&sum[1]==1&&sum[2]==0)//BG
		{
			printf("R\n");
		}
		else if(sum[0]==1&&sum[1]==0&&sum[2]==1)//BR
		{
			printf("G\n");
		}
		else if(sum[0]==0&&sum[1]==1&&sum[2]==1)//GR
		{
			printf("B\n");	
		}
		else 
		{
			if(sum[0]>1)
			{
				if(sum[1]==1&&sum[2]==0)
					printf("GR\n");
				else if(sum[1]==0&&sum[2]==1)
					printf("GR\n");
				else
					printf("BGR\n");
			}
			else if(sum[1]>1)
			{
				if(sum[0]==1&&sum[2]==0)
					printf("BR\n");
				else if(sum[0]==0&&sum[2]==1)
					printf("BR\n");
				else 
				    printf("BGR\n");
			}
			else if(sum[2]>1)
			{
				if(sum[0]==1&&sum[1]==0)
					printf("BG\n");
				else if(sum[0]==0&&sum[1]==1)
					printf("BG\n");
				else 
					printf("BGR\n");
			}
			else
				printf("BGR\n");
		}
    }
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值