<Sicily>Threecolor problem
来源:互联网 发布:vscode支持xp吗 编辑:程序博客网 时间:2024/05/17 01:12
一、题目描述
有红黄蓝3种颜色的n个珠子,师傅希望悟空把它们排成红色珠子在左,黄色珠子居中,蓝色珠子在右的一行,然后告诉师傅,从左数起,第m个珠子是什么颜色。众所周知,悟空是只猴子,他没有这个耐心,你帮帮他吧。
二、输入
输入第一行为一个整数t(0
三、输出
为每个测试用例单独一行输出指定的珠子颜色。
例如:
输入:
2
3 1
bry
4 3
rbyb
输出:
r
b
四、解题思路
这道题很水,只需要对输入的按照红、黄、蓝的次序进行排序,就可以了。这里使用的是插入排序(因为插入排序简单)。
五、代码
#include<iostream>using namespace std;int main(){ int times; cin >> times; while(times--) { int num, goal; cin >> num >> goal; char charArray[num]; for(int i = 0; i < num; i++) { char newChar; cin >> newChar; charArray[i] = newChar; } for(int i = 1; i < num; i++) { int j = i - 1; char temp; temp = charArray[i]; while(j >= 0) { if(charArray[j] == 'r') break; if(charArray[j] == 'y' && temp != 'r') break; if(charArray[j] == 'b' && temp == 'b') break; charArray[j+1] = charArray[j]; j--; } charArray[j+ 1] = temp; } cout << charArray[goal - 1] << endl; } return 0;}
0 0
- <Sicily>Threecolor problem
- <OJ_Sicily>Threecolor Problem
- Sicily 1902. Counting Problem
- Sicily 1902. Counting Problem
- Sicily 1791. Philia's Problem
- Sicily 1792. Hengheng's Problem
- Sicily 1157. The hardest problem
- Sicily 1791. Philia's Problem
- sicily--1157. The hardest problem
- Sicily 1085.Longge's problem
- sicily 1085. Longge's problem
- sicily 1157.The hardest problem
- Sicily 1157. The hardest problem
- Sicily 1157 the hardest problem
- Sicily 1085. Longge's problem
- [sicily]1157. The hardest problem
- sicily 1394 Root of the Problem
- Sicily 1394. Root of the Problem
- 【bzoj2753】[SCOI2012]滑雪与时间胶囊 最小生成树
- gradle
- Android App安装成系统应用
- 几何学
- 你应当如何学习C++(以及编程)
- <Sicily>Threecolor problem
- android.support.design.widget.TabLayout
- 坚持,坚持每天学习,短时间的快速学习
- Python中Dict与Set对比总结
- 为什么要少用Iframe
- IOS swift 自定义Tableview cell
- python Decorator模式
- 基于数据驱动的框架具体的学习点
- 构建android上多媒体apk