Arrange 0's & 1's in a array

前端 未结 16 2047
独厮守ぢ
独厮守ぢ 2020-12-04 13:36

This is one of an interview question which I had recently. I would like to know others perception of approach for this problem.

Question:

Yo

16条回答
  •  长情又很酷
    2020-12-04 14:19

    #include
    //#include
    
    int main()
    {
      int i,a[20]={0};
      int *ptr1,*ptr2;
      //clrscr();
      //a[2]=a[4]=a[6]=a[8]=a[16]=1;
      a[19]=a[18]=1;
    
      for(i=0;i<20;i++)
        printf("%d",a[i]);
    
      printf("\n\nafter");
    
      ptr1=ptr2=a;
      for(i=0;i<20;i++)
      {
        if(*ptr1==0&&*ptr2==1)
        {
          int temp=*ptr1;*ptr1=*ptr2;*ptr2=temp;
          ptr1++;ptr2++;
        }
        else if(*ptr1==1&&*ptr2==0)
        {
          ptr1++;ptr2++;
        }
        else if(*ptr1==0&&*ptr2==0)
        {
          ptr2++;
        }
        else
        {
          if(ptr1

提交回复
热议问题