三色旗的问题

来源:互联网 发布:淘宝上的微淘怎么发布 编辑:程序博客网 时间:2024/05/01 08:18

三色旗的问题

#include <stdio.h>#include <string.h>#define swap(x,y) {char temp;temp = color[x];color[x] = color[y];color[y] = temp;}int main(void){char color[] = "rwbbrrwbrbwbrwrbwbrbwb";int red=0;int white=0;int blue=strlen(color)-1;puts(color);while(white<blue){if(color[white]=='w'){white++;}else if(color[white]=='r'){swap(white,red);red++;white++;}else{blue--;swap(blue,white);}}puts(color);return 0;}


1 0