现在的位置: 首页 > 综合 > 正文

荷兰旗问题

2013年11月19日 ⁄ 综合 ⁄ 共 640字 ⁄ 字号 评论关闭

直接贴代码吧,也不知道当时是怎么想出来的,一下就想到这么做了

Code:
  1. redc = 0;   
  2. whitec = N - 1;   
  3.   
  4. for (i=0; i<=whitec;)   
  5. {   
  6.     if (pot[i] == 1)//当颜色为红色时,与前面的元素交换,然后判断下一位   
  7.     {   
  8.         if (i > redc)   
  9.         {   
  10.             temp = pot[i];   
  11.             pot[i] = pot[redc];   
  12.             pot[redc] = temp;   
  13.         }   
  14.         redc++;   
  15.         i++;   
  16.     }   
  17.   
  18.     if (pot[i] == 2)//当颜色为蓝色时,不变,判断下一位   
  19.     {   
  20.         i++;   
  21.         continue;   
  22.     }   
  23.     if (pot[i] == 3)//当颜色为白色时,与后面的元素交换,仍然判断第i个元素   
  24.     {   
  25.         if (whitec > i)   
  26.         {   
  27.             temp = pot[i];   
  28.             pot[i] = pot[whitec];   
  29.             pot[whitec] = temp;   
  30.         }   
  31.         whitec--;   
  32.     }   
  33. }   

 

抱歉!评论已关闭.