天天看點

網易筆試題兩道趕去公司調整隊形

趕去公司

終于到周末啦!小易走在市區的街道上準備找朋友聚會,突然伺服器發來警報,小易需要立即回公司修複這個緊急bug。假設市區是一個無限大的區域,每條街道假設坐标是(X,Y),小易目前在(0,0)街道,辦公室在(gx,gy)街道上。小易周圍有多個計程車打車點,小易趕去辦公室有兩種選擇,一種就是走路去公司,另外一種就是走到一個計程車打車點,然後從打車點的位置坐計程車去公司。每次移動到相鄰的街道(橫向或者縱向)走路将會花費walkTime時間,打車将花費taxiTime時間。小易需要盡快趕到公司去,現在小易想知道他最快需要花費多少時間去公司。

思路

各算各的即可求解

代碼

public class First {
    public  static  void  main(String [] arg)
    {
        Scanner scanner = new Scanner( System.in);
        int a = scanner.nextInt();
        int x[] = new int[a];
        int y[] = new int[a];
        int i=0,j=0;
        while (i<a)
        {
            x[i++]=scanner.nextInt();
        }

        while (j<a)
        {
            y[j++]=scanner.nextInt();
        }


        int gx = scanner.nextInt();
        int gy = scanner.nextInt();
        int wt = scanner.nextInt();
        int tt = scanner.nextInt();
        System.out.println(solution(x, y, gx, gy, wt, tt));
    }

   static public  int solution(int x[] ,int y [] , int gx,int gy,int walktime,int taxitime)
    {

        int all = Math.abs(gx*walktime)+Math.abs(gy*walktime);
        int  []time = new int[x.length];
        for ( int i = 0 ; i <x.length;i++)
        {
            int wt= Math.abs(x[i]*walktime) +Math.abs(y[i]*walktime);
            int tt = Math.abs(gx-x[i])*taxitime+Math.abs(gy-y[i])*taxitime;
            time[i]=wt+tt;
        }
        Arrays.sort(time);
        if (time[0]<all)
        return  time[0];
        return all;
    }
}
           

調整隊形

在幼稚園有n個小朋友排列為一個隊伍,從左到右一個挨着一個編号為(0~n-1)。其中有一些是男生,有一些是女生,男生用’B’表示,女生用’G’表示。小朋友們都很頑皮,當一個男生挨着的是女生的時候就會發生沖突。作為幼稚園的老師,你需要讓男生挨着女生或者女生挨着男生的情況最少。你隻能在原隊形上進行調整,每次調整隻能讓相鄰的兩個小朋友交換位置,現在需要盡快完成隊伍調整,你需要計算出最少需要調整多少次可以讓上述情況最少。例如:

GGBBG -> GGBGB -> GGGBB

這樣就使之前的兩處男女相鄰變為一處相鄰,需要調整隊形2次

思路

有兩種排序方式男孩在前,或者女孩在前,我們可以計算一下,他們的移動次數

代碼

public class Two {
    public  static  void  main(String [] arg)
    {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String a = scanner.next();
            System.out.println(solution1(a));
        }
    }
    static  public  int solution(String a)
    {
        int test= 0 ,test1=0;
        char[] c=a.toCharArray();
        char[] c1 =a.toCharArray();
        for ( int i=0;i<c.length;i++)
            for (int j = 0 ;j<c.length-1;j++)
            {
                if (c[j]=='B'&&c[j+1]=='G')
                {
                    char x= c[j];
                    c[j]=c[j+1];
                    c[j+1]=x;
                    test++;
                }
            }
        System.out.println(Arrays.toString(c));
        for ( int i=0;i<c1.length;i++)
            for (int j = 0 ;j<c1.length-1;j++)
            {
                if (c1[j]=='G'&&c1[j+1]=='B')
                {
                    char x= c1[j];
                    c1[j]=c1[j+1];
                    c1[j+1]=x;
                    test1++;
                }
            }
        System.out.println(Arrays.toString(c1));
        if (test<test1)
        return test;
        else
            return test1;
    }
    static  public  int solution1(String a)
    {
        char[] c =a.toCharArray();
        int left=0,left1=0;
        int right=0,right1=0;
        for (int i =0;i<c.length;i++)
        {
            if (c[i]=='G')
                left++;
            else
                left1+=left;
        }
        for (int i =0;i<c.length;i++)
        {
            if (c[i]=='B')
                right++;
            else
                right1+=right;
        }
        return left1>right1?right1:left1;

    }
    static  public  int solution1(String a)
    {
        char[] c =a.toCharArray();
        int left=0,left1=0;
        int right=0,right1=0;
        /*
        比如說GGGBG
        下面是将B放在前面,然後我們不用排序,因為B前面有3個G
        是以就需要向前三次
         */
        for (int i =0;i<c.length;i++)
        {
            if (c[i]=='G')
                left++;             //記錄前面有多少個G
            else                    //出現了B,那麼将這個B轉移到前面就需要
                left1+=left;        //這麼多次操作
        }

//        類似的;
//        比如說BGBGBG;
//        第一個G前面有一個B,移動一次
//        第二個G前面有兩個B,就需要移動兩次

        for (int i =0;i<c.length;i++)
        {
            if (c[i]=='B')
                right++;
            else
                right1+=right;
        }
        return left1>right1?right1:left1; //比較一下那種移動次數少,就傳回喽

    }
}
           

收獲

  1. 在第二題的時候,冒泡寫錯了;基礎知識不紮實啊!
  2. 題還是挺簡單的,認真做,想辦法用簡單的辦法做出來;