google编程挑战赛Round1的前两道题

差了一点,看来的确不适合这项运动了。

        public int chooseSeat(String row)
        
{
            
int[] min=new int[row.Length];
            
for(int i=0;i<row.Length-1;i++)
            
{
                min[i]
=IsOk(row,i);
            }

            
int j=-1;
            
int mine=-1;
            
for(int i=0;i<row.Length-1;i++)
            
{
                
if(min[i]>mine)
                
{
                    j
=i;
                    mine
=min[i];
                }

            }

            
return j;
        }

        
public int IsOk(string row,int i)
        
{
            
if(row[i]!='X'&&row[i+1]!='X')
            
{
                
int l1=i;
                
int l2=row.Length-i-1;
                
return l1<l2?l1:l2;
            }

            
else
                
return -1;
        }



        
public int cover(int[] heights)
        
{
            
if(heights.Length==1)return 1;
            
int imax=-1;
            
int max=-1;
            
for(int i=0;i<heights.Length;i++)
            
{
                
if(heights[i]>max)
                
{
                    imax
=i;
                    max
=heights[i];
                }

            }

            
if(lbstate(heights,imax)==-1)
            
{
                heights[imax]
--;
                
return cover(heights);
            }

            
if(lbstate(heights,imax)==1)
            
{
                heights[imax]
--;
                
return cover(heights)+1;
            }

            
if(lbstate(heights,imax)==0)
            
{
                
int[] h2=new int[heights.Length-1];
                
int j=0;
                
for(int i=0;i<heights.Length;i++)
                
{
                    
if(i!=imax+1)
                    
{
                        h2[j]
=heights[i];
                        j
++;
                    }

                }

                
return cover(h2);
            }

            
return 0;

        }

        
public int lbstate(int[] h,int idx)
        
{
            
if(idx-1>=0)
            
{
                
if(h[idx-1]==h[idx])return 0;
            }

            
if(idx+1<h.Length)
            
{
                
if(h[idx+1]==h[idx])return 0;
            }

            
if(idx-1>=0)
            
{
                
if(h[idx-1]==h[idx]-1)return 1;
            }

            
if(idx+1<h.Length)
            
{
                
if(h[idx+1]==h[idx]-1)return 1;
            }

            
return -1;
        }
posted @ 2005-12-19 23:51  jetz  阅读(323)  评论(0编辑  收藏  举报