C语言三色旗问题(考试最后一道编程题)

xiaya1986cv 2008-04-29 09:26:00
三色旗问题,一个字符串Color,其中每个元素值为‘R’‘W’‘B’三者之一,实现把数组中元素重新排列,所有蓝色在前,白色其后,红最后。
编写这么一个程序。
网上找到以下算法:

其實要實行這演算法抓住這幾個原則:
在 0 到 b - 1 之間放藍色,b 到 w - 1 放白色,r + 1 到 n - 1 放紅色,而 w 到 r 之間元素未曾處理...
每一次都處理 w 所在元素,有三種情形:
1. 如果 w 所在位置是白色:那就把 w 加上 1
2. 如果 w 所在元素是藍色:把 b 與 w 所在元素對調,b、w 各加 1,維持上圖
3. 如果 w 所在元素是紅色:就把 w 和 r 元素互換,但是 r 減 1,w 不需減 1

看不太懂,这个程序应该怎么写,请高人指点
...全文
793 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
DreamMakers 2012-05-08
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

#define BLUE 'b'
#define WHITE 'w'
#define RED 'r'
#define swap(x,y) {char temp;temp=*(point+x);*(point+x)=*(point+y);*(point+y)=temp;}

int _tmain(int argc, _TCHAR* argv[])
{
int N;
int bflag=0,wflag=0,rflag;
cout<<"please input number of characters:"<<endl;
cin>>N;
char *point=new char[N];
cout<<"please input the string:"<<endl;
cin>>point;
rflag=strlen(point)-1;
cout<<"the origin string is :"<<endl;
cout<<point<<endl;
while(wflag<=rflag)
{
if(point[wflag]==WHITE)
{
wflag++;
}
else if(point[wflag]==BLUE)
{
swap(wflag,bflag);
wflag++;bflag++;
}
else
{
while(wflag<rflag&&point[rflag]==RED)
rflag--;
swap(wflag,rflag);
rflag--;
}
}
cout<<"the ordered string is :"<<endl;
cout<<point<<endl;
return 0;
}
legendtao 2010-03-01
  • 打赏
  • 举报
回复
20楼正解,。。。。。。。。。。。。。。。。。。。。
legendtao 2010-03-01
  • 打赏
  • 举报
回复
引用 20 楼 pseudocodes 的回复:
C/C++ codeint dutch_flag(int color[],int n )
{int white=0 ;int blue=0 ;int red= n-1 ;int t=0 ;while(white<= red)/* is there are more token*/
{if(color[white]== WHITE)/* is a white*/
white++ ;/* Yes just inc. white ptr*/elseif(color[white]== BLUE )/* is a blue*/
{
swap(color[blue],color[white]); t++;/* Yes ,swap the last w*/
blue++;/* and the last b and inc.*/
white++ ;
}else/* or is it a red ,skip*/
{while(white< red&& color[red]== RED)
red--;/* red token and*/
swap(color[red],color[white]); t++;/* swap with the last w*/
red-- ;/* dec red ptr*/
}
}return t ;
}


有BUG 提供数据rrbwrbbrwrbwrbwbr
legendtao 2010-03-01
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

#define WHITE 'w'
#define RED 'r'
#define BLUE 'b'

int main()
{
char flag[80] ;
char temp;
int white, red, blue;
gets(flag);
white = blue = 0;
red = strlen(flag)-1;

while(white<red)
{
if(flag[white] == WHITE)
white++;
else if(flag[white] == BLUE)
{
temp = flag[white];
flag[white] = flag[blue];
flag[blue] = temp;
white ++;
blue ++;
}
else
{
while(white<red && flag[red]==RED)
red --;
temp = flag[white];
flag[white] = flag[red];
flag[red] = temp;

}
}
puts(flag);
return 0;
}
fxzxxb 2008-05-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gzg25895381 的回复:]
main()
{
int i,r,w,b;
r=0;w=0;b=0;
char c[20];
printf("Please input the color:\n");
scanf("%s",c);
for(i=0;i <20;i++)
{
if(c[i]=='R')
r+=1;
if(c[i]=='W')
w+=1;
if(c[i]=='B')
b+=1;
if(c[i]='\0')
break;
}
for(i=1;i <b;i++)
printf("B");
for(i=1;i <w;i++)
printf("W");
for(i=1;i <r;i++)
...如果理解正确的话,这样做无疑是最简单化了,很明了...
[/Quote]
nikorliu 2008-05-12
  • 打赏
  • 举报
回复
我指挥用冒泡排序法^_^
pseudocodes 2008-05-12
  • 打赏
  • 举报
回复
5楼的人真无聊,这道题是Dijkstra提出来的,要求就是不能用额外空间 以及交换次数最少

另补充一下
#define BLUE 3
#define WHITE 2
#define RED 1

int i , j , k ,n,t;
int a[10010];

void swap(int &a, int &b)
{
int temp ;
temp = a ;
a = b ;
b = temp;
}
pseudocodes 2008-05-12
  • 打赏
  • 举报
回复

int dutch_flag(int color[], int n )
{
int white = 0 ;
int blue = 0 ;
int red = n - 1 ;
int t = 0 ;

while(white <= red) /* is there are more token*/
{
if(color[white] == WHITE) /* is a white */
white ++ ; /* Yes just inc. white ptr */
else if(color[white] == BLUE ) /* is a blue */
{
swap(color[blue],color[white]); t++; /* Yes ,swap the last w */
blue ++; /* and the last b and inc. */
white ++ ;
}
else /* or is it a red ,skip */
{
while(white < red && color[red] == RED)
red --; /* red token and */
swap(color[red],color[white]); t ++; /* swap with the last w */
red -- ; /* dec red ptr */
}
}
return t ;
}
zbing0203 2008-05-12
  • 打赏
  • 举报
回复
char str[]="RWRRWBBRWBWR";
int len=strlen(str);
int i=0,j=0;
char t;
for(i=0;i<=len;i++)
for(j=0;j<len-i;j++)
{
if (str[j+1] == '\0')
continue;
if ((str[j] == 'R') && (str[j+1] != 'R'))
{
t=str[j];
str[j]=str[j+1];
str[j+1]=t;
continue;
}
else if ((str[j] == 'W') && (str[j+1] == 'B'))
{
t=str[j];
str[j]=str[j+1];
str[j+1]=t;
continue;
}
else if ((str[j] == 'B'))
continue;
else if (str[j] == str[j+1])
continue;
}

printf("%s\n",str);
return 0;[/code]
chlaws 2008-05-11
  • 打赏
  • 举报
回复
three_color_flag
q84316577 2008-05-11
  • 打赏
  • 举报
回复
我基础差了点

弄不懂意思
xiaya1986cv 2008-05-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 afteryou_7 的回复:]
我想
原题的意义在于不开辟新的内存空间
而在原空间上排序
不然没什么意义
[/Quote]
同意楼上的意见,应该有以下的限制
寫時必須滿足下列限制:
1. 不用到額外 Memory,意思就是只能在陣列之內互換方式完成
2. 互換兩元素動作越少越好
3. 對每一元素而言,測試他是 R、W、B 的工作,每種顏色最多只能一次
afteryou_7 2008-05-10
  • 打赏
  • 举报
回复
我想
原题的意义在于不开辟新的内存空间
而在原空间上排序
不然没什么意义
lysggenius 2008-05-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gzg25895381 的回复:]
main()
{
int i,r,w,b;
r=0;w=0;b=0;
char c[20];
printf("Please input the color:\n");
scanf("%s",c);
for(i=0;i <20;i++)
{
if(c[i]=='R')
r+=1;
if(c[i]=='W')
w+=1;
if(c[i]=='B')
b+=1;
if(c[i]='\0')
break;
}
for(i=1;i <b;i++)
printf("B");
for(i=1;i <w;i++)
printf("W");
for(i=1;i <r;i++)

[/Quote]
这个就不错嘛
xwqi003 2008-05-01
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "string.h"
#include "conio.h"
#define N 100

void main()
{
char color[N],temp;
int i,j;
printf("input color R or W or B:");
gets(color);

for(i=1;i<N;i++)
{
for(j=N-1;j>=i;j--)
{
if(color[j]!='\0')
{
if(color[j-1]>color[j])
{
temp=color[j-1];
color[j-1]=color[j];
color[j]=temp;
}
}
}
}
for(i=0;i<N;i++)
{
if(color[i]=='W')
{
color[i]='R';
continue;
}
if(color[i]=='R')
{
color[i]='W';
}
}
for(i=0;i<N;i++)
{
if(color[i]=='B'||color[i]=='W'||color[i]=='R')
{
printf("%s",color[i]);
}
}
getch();
}
gzg25895381 2008-04-29
  • 打赏
  • 举报
回复
main()
{
int i,r,w,b;
r=0;w=0;b=0;
char c[20];
printf("Please input the color:\n");
scanf("%s",c);
for(i=0;i<20;i++)
{
if(c[i]=='R')
r+=1;
if(c[i]=='W')
w+=1;
if(c[i]=='B')
b+=1;
if(c[i]='\0')
break;
}
for(i=1;i<b;i++)
printf("B");
for(i=1;i<w;i++)
printf("W");
for(i=1;i<r;i++)
printf("R")
}
不想低调 2008-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 babyvox1999 的回复:]
换来换去多麻烦,你的排列是死的,直接数个数
[/Quote]

up
qmm161 2008-04-29
  • 打赏
  • 举报
回复
简单点,排序!
babyvox1999 2008-04-29
  • 打赏
  • 举报
回复
换来换去多麻烦,你的排列是死的,直接数个数
xiaya1986cv 2008-04-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jennyvenus 的回复:]
题目不懂。
[/Quote]
例如Color里面的是RWRRWBBRWBWR的话,程序运行后变为BBBWWWWRRRRR
加载更多回复(4)

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧