搜狗秋招筆試第三題吃糖代碼

思路:

小明和小紅都想要贏,所以他們每次都會選當下最多的糖盒,一開始的時候小明會選全局最大的,然後就有了斷點,以後就從每一次的斷點的兩邊取糖盒,所以比較的就是左和右的大小,每一次取走糖盒,就把環重新排成新的環,然後再進行此過程,直到沒有糖盒。

最後比較小明和小紅拿的糖的大小,計算差值,就好啦

 

package test;
 
import java.util.Scanner;
 
public class Sougou{
     
    public static int solution(int [] array){
         
        int xiaoming=0;
        int xiaohong=0;
         
        int cutIndex=max(array);   //定義切點,首先取第一個切點(小明先拿)。
         
        xiaoming=xiaoming+array[cutIndex];
         
        array[cutIndex]=0;    //先選掉最大的那盒,變成環狀。
         
        array=transform(array);   //transform方法是每次拿走一個糖盒之後,把數組重新排成新的數組。可以理解爲形成新的環。
         
        int num=array.length;
        for(int i=0,j=1;i<num;i++,j++){    //num是一共還有多少個糖盒,就循環取多少次
            int maxIndex;//最大值的下標
             
            if(cutIndex==array.length) {                     //最大值下標如果是新數組長度,例如新數組長度是4,下標有0123,maxIndex是4,說明上一次取走的糖盒是4,這種情況的左和右是array.length-1和0;             
                 maxIndex=maxLeftOrRight(array,array.length-1,0);
            }else if(cutIndex==0){                              //最大值下標如果是0,那麼說明上次取走的是數組第一個,那麼數組重新組合後,曾經在1位置的數左移到了0位置,那左和右分別是array.length-1(數組最後一個數)和cutIndex=0;
                maxIndex=maxLeftOrRight(array,array.length-1,cutIndex);
            }else if(cutIndex==array.length-1){                //最大值下標如果是最後一個數,說明上一次取走的是數組倒數第二個數,那左和右就分別爲cutIndex-1和cutIndex。
                maxIndex=maxLeftOrRight(array,cutIndex-1,cutIndex);
            }else{                                            //其他情況,表示的就是在中間那些被取走的糖盒,他們不屬於特殊情況,只需要取左AND右就行。
                maxIndex=maxLeftOrRight(array,cutIndex-1,cutIndex);
            }
             
            if(j%2==1){                                            //定義一個J值,從1開始,因爲小明和小紅循環取糖盒,所以循環每人加數字。
                xiaohong=xiaohong+array[maxIndex];
                array[maxIndex]=0;
                cutIndex=maxIndex;
                array=transform(array);
            }else if(j%2==0){
                xiaoming=xiaoming+array[maxIndex];
                array[maxIndex]=0;
                cutIndex=maxIndex;
                array=transform(array);
            }
             
        }
         
        int result=xiaoming>xiaohong?xiaoming-xiaohong:xiaohong-xiaoming;
         
        return result;
    }
     
    public static int [] transform(int [] array){      //每次取值後,因爲被取走的位置是0,所以應該把數組轉換爲新的環,去掉0值,就是這個過程。
        int []  temp=new int [array.length];
        for(int i=0,j=0;i<array.length;i++){
            if(array[i]!=0){
                temp[j]=array[i];
                j++;
            }
        }
          int count=0;
        for(int i=0;i<temp.length;i++){
            if(temp[i]!=0){
                count++;
            }
        }
        int [] newArray=new int [count];
        for(int i=0;i<count;i++){
            newArray[i]=temp[i];
        }
         
        return newArray;
    }
     
    public static int max(int [] array){    //由於小明和小紅都想贏,所以在最開始小明肯定選最大的,這個Max是選最大的過程。
        int index=0;
        for(int i=0;i<array.length;i++){
            index=array[i]>array[index]?i:index;
        }
        return index;
    }
     
    public static int maxLeftOrRight(int [] array ,int left,int right){    //在左和右中選最大的過程
        return array[left]>array[right]?left:right;
    }
     
    public static void main(String[] args){
         
        Scanner sc=new Scanner(System.in);
         
        int n=sc.nextInt();
         
        int [] array= new int [n];
         
        for(int i=0;i<array.length;i++){
            array[i]=sc.nextInt();
        }
         
        int result=solution(array);
        System.out.println(result);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章