Codeforces 626B Cards(规律)
2016-02-15 00:19
381 查看
B. Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
input
output
Note
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三种,两张不动颜色的卡片合成一张第三种颜色的卡片,两张相同颜色的卡片合成该颜色的一张卡片。 按照此规则,任意组合,输出最后一张卡片的颜色,输出所有可能。
题解:规律题。①当n张卡片只有一种颜色,最后一张肯定就是该颜色。
②当n(n>2)张卡片里有两种颜色,A有(n-1)张,B只有1张,那么结果可以为B,C两种颜色。
③当n==2,且有A,B颜色卡片各一张,则最后颜色为C颜色。
④其他情况均有A,B,C这三种颜色。
代码如下(看了外国菊苣写的,我写的码真渣):
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
2 RB
output
G
input
3
GRG
output
BR
input
5 BBBBB
output
B
Note
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三种,两张不动颜色的卡片合成一张第三种颜色的卡片,两张相同颜色的卡片合成该颜色的一张卡片。 按照此规则,任意组合,输出最后一张卡片的颜色,输出所有可能。
题解:规律题。①当n张卡片只有一种颜色,最后一张肯定就是该颜色。
②当n(n>2)张卡片里有两种颜色,A有(n-1)张,B只有1张,那么结果可以为B,C两种颜色。
③当n==2,且有A,B颜色卡片各一张,则最后颜色为C颜色。
④其他情况均有A,B,C这三种颜色。
代码如下(看了外国菊苣写的,我写的码真渣):
#include<cstdio> #include<cstring> char str[210]; int main() { int n,r,b,g,i; while(scanf("%d",&n)!=EOF) { scanf("%s",str); r=0; b=0; g=0; for(i=0;i<n;++i) { if(str[i]=='R') r++; if(str[i]=='G') g++; if(str[i]=='B') b++; } if(r>0&&g==0&&b==0) printf("R\n"); else if(r==0&&g>0&&b==0) printf("G\n"); else if(r==0&&g==0&&b>0) printf("B\n"); else if(r==0&&g==1&&b==1) printf("R\n"); else if(r==1&&g==0&&b==1) printf("G\n"); else if(r==1&&g==1&&b==0) printf("B\n"); else { if(r>1) { if(g==1&&b==0) printf("BG\n"); else if(g==0&&b==1) printf("BG\n"); else printf("BGR\n"); } else if(g>1) { if(r==1&&b==0) printf("BR\n"); else if(r==0&&b==1) printf("BR\n"); else printf("BGR\n"); } else if(b>1) { if(g==1&&r==0) printf("GR\n"); else if(g==0&&r==1) printf("GR\n"); else printf("BGR\n"); } else printf("BGR\n"); } } return 0; }
相关文章推荐
- 1050. String Subtraction (20)
- HDU-ACM-2041
- 欢迎使用CSDN-markdown编辑器
- mini售票系统之创建线程的三种方式
- android proxy settings with lantern
- 常用的三类IP地址
- JQuery直接调用asp.net后台WebMethod方法
- neuq oj 1019: 求和again 并非求和题C++
- hdu1272 并查集
- 操作系统学习2
- ubuntu安装软件
- H3C交换系列之isolate-user-vlan
- 自定义表单(二)--拖拽(HTML版本)
- Spring MVC 中 HandlerInterceptorAdapter的使用
- leetcode 328 Odd Even Linked List
- [Python入门]Chapter1 编程方式
- 1049. Counting Ones (30)
- 欢迎使用CSDN-markdown编辑器
- 将Word文档发给别人时如何限制别人只能修改文档部分内容
- android View的点击事件分发机制的简单认知