其他分享
首页 > 其他分享> > SWUST OJ492: 荷兰国旗问题

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;
}

标签:tmp,国旗,++,int,num,printf,OJ492,SWUST,500005
来源: https://blog.csdn.net/weixin_63587703/article/details/122608030