描述:假设数组元素只能取0,1,2三个值,那么将数组调整为00...11..22形式输出(类似于荷兰国旗的三种颜色排列)
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int a[] ={1,0,0,2,1,0,2,1,0,1};
int sum = 10,cnt = 0;
void swap(int i,int j){
int t = a[i];a[i] = a[j];a[j] = t;
}
int main(){
int begin = 0,cur = 0,end = 9;
while(cur<=end){
if(a[cur]==2){
swap(cur,end);
end--;
}else if(a[cur]==1) {
cur++;
}else{
if(begin!=cur)
swap(begin,cur);
cur++;begin++;
}
}
for(int i = 0;i < 10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}