两道算法题

1.  取到7个数,0可以重复,其他不重复,0可以充当任意数,判断7个数里面是否存在5个连续数  
    1,5,2,4,0,13,10     0->3   1,2,3,4,5,10,13   return   true  
    1,5,2,0,0,13,10     0->3   0->4   1,2,3,4,5,10,13   return   true  
    1,5,6,0,0,13,10     return   false  

 

    Module   CheckNumber  
          Public   Function   iscontinuousNumbers(ByVal   NumbersString   As   String)   As   Boolean  
                  Dim   continuousCount   As   Integer   =   1  
                  Dim   zeroCount   As   Integer  
                  Dim   noZeroString   As   String   =   Replace(Replace(NumbersString,   “,0”,   “”),   “0,”,   “”)  
                  zeroCount   =   CInt(CInt(Len(NumbersString)   –   Len(noZeroString))   /   2)  
                  Dim   numbers()   As   String   =   Split(noZeroString,   “,”)  
   
                  For   i   As   Integer   =   0   To   UBound(numbers)   –   1  
                          For   j   As   Integer   =   0   To   UBound(numbers)   –   i   –   1  
                                  Dim   savenumber   As   String  
                                  If   CInt(numbers(j))   >   CInt(numbers(j   +   1))   Then  
                                          savenumber   =   numbers(j)  
                                          numbers(j)   =   numbers(j   +   1)  
                                          numbers(j   +   1)   =   savenumber  
                                  End   If  
                          Next  
                  Next  
   
                  Dim   recCurrentContinuous   As   Integer   =   zeroCount  
                  For   i   As   Integer   =   1   To   UBound(numbers)  
                          Dim   continuousTwoNumbers   As   Integer   =   CInt(numbers(i))   –   CInt(numbers(i   –   1))  
                          If   continuousTwoNumbers   –   1   <=   recCurrentContinuous   Then  
                                  continuousCount   +=   continuousTwoNumbers  
                                  recCurrentContinuous   -=   (continuousTwoNumbers   –   1)  
                          Else  
                                  continuousCount   =   1  
                                  recCurrentContinuous   =   zeroCount  
                          End   If  
                          If   continuousCount   >=   5   Then  
                                  Return   True  
                          End   If  
                  Next  
   
                  Return   False  
          End   Function  
  End   Module  

2. 根据上排给出十个数,在其下排填出对应的十个数

要求下排每个数都是先前上排对应那个数在下排十个数中出现的次数。

上排的十个数如下:

【0,1,2,3,4,5,6,7,8,9】

 

public class Test  

{  

  public static void main(String[] args)  

  {  

    NumberTB nTB = new NumberTB(10);  

      

    int[] result = nTB.getBottom();  

    for(int i=0;i<result.length;i++)  

    {  

      System.out.print(result[i] + ” “);  

    }  

  }  

}  

 

class NumberTB  

{  

  private int[] top;  

  private int[] bottom;  

  private int len;  

  private boolean success;  

    

  //please into len >= 4  

  public NumberTB(int len)  

  {  

    this.len = len <= 4 ? 4 : len;  

    this.success = false;  

      

    this.top = new int[this.len];  

    this.bottom = new int[this.len];  

      

    //format top  

    for(int i=0;i<this.len;i++)  

    {  

      this.top[i] = i;  

    }  

  }  

    

  public int[] getBottom()  

  {  

    int i = 0;  

      

    while(!this.success)  

    {  

      i++;  

      setNextBottom();  

    }  

      

    System.out.println(“执行了: ” + i + “次循环得到结果”);  

      

    return this.bottom;  

  }  

    

  //set next bottom  

  private void setNextBottom()  

  {  

    boolean reB = true;  

      

    for(int i=0;i<this.len;i++)  

    {  

      int frequecy = getFrequecy(i);  

        

      if(this.bottom[i] != frequecy)  

      {  

        this.bottom[i] = frequecy;  

        reB = false;  

      }  

    }  

      

    this.success = reB;  

  }  

    

  //get frequency in bottom  

  private int getFrequecy(int num)  

  {  

    int count = 0;  

      

    for(int i=0;i<this.len;i++)  

    {  

      if(this.bottom[i] == num)  

        count++;  

    }  

      

    return count;  

  }  

} 

点赞