太懶了 畢竟這道題解法太明顯 直接上代碼吧
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iM5ITN5MGMiRWN4Y2MlhTNzYzXyMDOzUTM2IzLcBTMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
方法一:
#include <stdio.h>
#include <string.h>
int main()
{
int s[10];
int n;
int count=0;
int i=0;
int rr,nx,lb;
int temp;
printf("請輸入;0代表紅色,1代表白色,2代表藍色\n");
do{
scanf("%d",&s[i++]);
count++;
}while(getchar() != '\n');
rr=-1;
nx=0;
lb=count;
while(nx!=lb)
{
if(s[nx]==0)
{
rr++;
temp=s[nx];
s[nx]=s[rr];
s[rr]=temp;
nx++;
}else if(s[nx]==1)
{
nx++;
}else if(s[nx]==2)
{
lb--;
temp=s[nx];
s[nx]=s[lb];
s[lb]=temp;
}
}
for(i=0;i<count;i++) \\這裡實作輸出rbw
{
switch(s[i])
{
case 0:putchar('r');break;
case 1:putchar('w');break;
case 2:putchar('b');break;
}
}
puts("");
return(0);
}
while(nx!=lb)
{
switch(s[nx])
{
case 0: rr++;
temp=s[nx];
s[nx]=s[rr];
s[rr]=temp;
nx++;break;
case 1:nx++;break;
case 2:lb--;
temp=s[nx];
s[nx]=s[lb];
s[lb]=temp;break;
}
}
while(nx!=lb)
{
if(s[nx]==0)
{
rr++;
temp=s[nx];
s[nx]=s[rr];
s[rr]=temp;
nx++;
}
if(s[nx]==1)
{
nx++;
}
if(s[nx]==2)
{
lb--;
temp=s[nx];
s[nx]=s[lb];
s[lb]=temp;
}
}