poj 1147 Binary codes

//poj 1147 Binary codes
/*
就像大家说的,这题很有趣,可惜我天资愚钝,想了好久才有思绪。
http://blog.csdn.net/joy32812/archive/2010/06/27/5697056.aspx
*/
#include <iostream>
#include <algorithm>

using namespace std;

int a[3001],next[3001];
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
          int b=0,c=0; //b记录0的个数
          for(int i=1;i<=n;i++)
          {
                  scanf("%d",&a[i]);
                  if (a[i]==0) b++;
          }
          for (int i=1;i<=n;++i)
          if (a[i]) next[++b]=i;
          else next[++c]=i;
          c=1;
          for (int i=1;i<=n;i++)
          {
              c=next[c];
              printf("%d",a[c]);
              if (i<n) printf(" ");
          }
          printf("/n");
         
    }
    system("pause");
    return 0;
   
}

你可能感兴趣的:(poj 1147 Binary codes)