SWUST OJ492: 荷兰国旗问题

题目描述

荷兰国旗的问题是重新排列一系列字符R,W和B(红色,白色和蓝色是荷兰国旗的颜色),以便所有R首先出现,W紧随其后,B最后。针对此问题设计线性且稳定的算法

输入

two lines, the first line is total of numbers characters R,W and B ,and the numbers less than 500005
the second line is random characters R,W and B

输出

a line, all the R’s come first, the W’s come next, and the B’s come last.

样例输入

10
WRRWRWBBRW

样例输出

RRRRWWWWBB
#include<stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	char a[500005];
	char r[500005], w[500005], b[500005];
	int i;
	int count = 0;
	int num = 0;
	int tmp = 0;
	scanf("%s", a);
	for (i = 0; i < n; i++)
	{
		if (a[i] == 'R')
		{
			r[count++] = a[i];
		}
		if (a[i] == 'W')
		{
			w[num++] = a[i];
		}
		if (a[i] == 'B')
		{
			b[tmp++] = a[i];
		}
	}
	for (i = 0; i < count; i++)
	{
		printf("%c", r[i]);
	}
	for (i = 0; i < num; i++)
	{
		printf("%c", w[i]);
	}
	for (i = 0; i < tmp; i++)
	{
		printf("%c", b[i]);
	}
	printf("\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值