麻烦+找规律+各种情况
来源:互联网 发布:mm下载软件 编辑:程序博客网 时间:2024/03/28 20:04
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 lengthn — 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
2RB
G
3GRG
BR
5BBBBB
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");elseprintf("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");}elseprintf("BGR\n");} }return 0;}
- 麻烦+找规律+各种情况
- 找规律
- 找规律!
- 找规律
- 找规律,
- 找规律
- 找规律
- 找规律
- 找规律
- 找规律
- 找规律
- 找规律
- 找规律
- 找规律
- 无线网络,找我麻烦
- 递归(找规律、找出口)
- POJ 1082 Calendar Game 找规律博弈(存在特殊情况)
- 数字找规律
- 排序算法之希尔(优化冒泡)排序
- 如何赚到互联网的“下一桶金”
- 关于跳槽,是我心浮气躁?还是我确实该离开了?
- 线性表的链式存储及其接口函数C++类实现
- IO小练习
- 麻烦+找规律+各种情况
- 2016年3月5日CVTE网测的两道编程题
- React-Native中this的带给大家的困惑
- mysql中utf8_bin、utf8_general_ci、utf8_general_cs编码区别
- Linux内核分析,完成一个简单的时间片轮转多道程序内核代码
- java学习--内存泄露和内存溢出
- uva 10881(ants)
- table合并单元格colspan和rowspan
- 【ZYNQ-7000开发之十三】中断:私有定时器中断