[字節跳動]2018秋招算法題【持續更新中】

1.世界盃開幕式會在球場C舉行,球場C的球迷看臺可以容納M*N個球迷。在球場售票完成後,現官方想統計此次開幕式一共有多少個球隊球迷羣體,最大的球隊球迷羣體有多少人。

經調研發現,球迷羣體在選座時有以下特性:

同球隊的球迷羣體會選擇相鄰座位,不同球隊的球迷羣體會選擇不相鄰的座位(註解:相鄰包括前後相鄰,左右相鄰,斜對角相鄰)

給定一個M*N的二維球場,0代表該位置沒有坐人,1代表該位置已有選擇,希望輸出球隊羣體個數P,最大的球隊羣體人數Q

輸入描述:

第一行,2個數字,M及N,使用英文逗號分隔

接下來M行,每行N的數字,使用英文逗號分隔

輸出描述:

一行,2個數字,P及Q,使用英文逗號分隔

其中P表示球隊羣體個數,Q表示最大的球隊羣體人數

例:輸入

10,10

0,0,0,0,0,0,0,0,0,0

0,0,0,1,1,0,1,0,0,0

0,1,0,0,0,0,0,1,0,1

1,0,0,0,0,0,0,0,1,1

0,0,0,1,1,1,0,0,0,1

0,0,0,0,0,0,1,0,1,1

0,1,1,0,0,0,0,0,0,0

0,0,0,1,0,1,0,0,0,0

0,0,1,0,0,1,0,0,0,0

0,1,0,0,0,0,0,0,0,0

輸出:6,8

	import java.util.Scanner;
	
	public class BallFan {
	    public static int m = 0;
	    public static int n = 0;
	    public static int total = 0;
	
	    public static void main (String[] args){
	        Scanner sc = new Scanner(System.in);
	        String[] m_n = sc.nextLine().split(",");
	        m = Integer.parseInt(m_n[0]);//行
	        n = Integer.parseInt(m_n[1]);//列
	        if (m <= 0 || n <= 0) return;
	
	        int[] arr = new int[n*m + 1];//用一維數組存儲
	        int i = 1;
	        for(int j=0; j < m; j++){
	            String[] line = sc.nextLine().split(",");
	            for (int k=0; k < n; k++){
	                arr[i] = Integer.parseInt(line[k]);
	                i++;
	            }
	        }
	
	        int groups = 0;//組數
	        int max = 0;//最大人數
	        for (int j=1; j <= n*m; j++){
	            if (arr[j] == 0) continue;
	
	            groups++;
	            total = 0;
	            //遞歸求解
	            countMumber(arr, j);
	
	            if (total > max)
	                max = total;
	
	        }
	
	        System.out.println(groups + "," + max);
	
	    }
	
	    public static void countMumber (int[]arr, int poi){
	
	        if (arr[poi] == 0) return;
	
	        arr[poi] = 0;
	        total++;
	
	        //確定當前遊標位置,根據遊標位置取方位值
	        if (poi % n != 0){//最右並不需要向右邊尋值,我們可以認爲是一個從左到右的過程
	            countMumber(arr, poi+1);
	        }
	        if (poi % n != 1){//最左並不需要向左邊尋值
	            countMumber(arr, poi-1);
	        }
	
	        if (n*m - n > poi){//不位於下邊界即可向下探索
	            int down = poi + n;
	            countMumber(arr, down);//向下尋值
	
	            if (down % n != 0){//非最右
	                countMumber(arr, down+1);//下右對角線
	            }
	
	            if (down % n != 1){//非最左
	                countMumber(arr, down-1);//下左對角線
	            }
	
	        }
	        if (n < poi){
	            int up = poi - n;
	            countMumber(arr, up);//向上尋值
	
	            if (up % n != 0){//非最右
	                countMumber(arr, up+1);//上右對角線
	            }
	
	            if (up % n != 1){//非最左
	                countMumber(arr, up-1);//上左對角線
	            }
	        }
	
	    }
	
	}

2.爲了提高文章質量,每一篇文章(假設全部都是英文)都會有m名編輯進行審覈,每個編輯獨立工作,會把覺得有問題的句子通過下表記錄下來,比如[1,10],1表示病句的第一個字符,10表示病句的最後一個字符。也就是從1到10着10個字符組成的句子,是有問題的。

現在需要把多名編輯有問題的句子合併起來,送個總編輯進行最終的審覈。比如編輯A指出的病句是[1,10],[32,45];編輯B指出的病句是[5,16],[78,94]那麼[1,10]和[5,16]是有交叉的,可以合併成[1,16][32,45][78,94]

輸入描述:

編輯數量m,之後每行是每個編輯的標記的下表組合,第一個和最後一個下標用英文逗號分隔,每組下標之間用分號分隔

輸出描述:

合併後的下標集合,第一個和最後一個下標用英文逗號分隔,每組下標之間用分號分隔。返回結果是從小到大遞增排列

例:輸入

3

1,10;32,45

78,94;5,16

80,100;200,220;16,32

輸出: 1,45;78,100;200,220

	package test2;
	
	import java.util.*;
	public class ErrTopic {
	
	    public static void main (String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int lines = Integer.parseInt(sc.nextLine());
	        HashMap <Integer,Integer> hs = new HashMap<>();
	
	        int groups = 1;
	        for (int i=0; i < lines; i++){
	            String[] finds = sc.nextLine().split(";");
	            for (String area: finds){
	                String[] deatil = area.split(",");
	                int begin = Integer.parseInt(deatil[0]);
	                int end = Integer.parseInt(deatil[1]);
	                hs.put(begin, end);
	                groups++;
	            }
	        }
	
	        int[] arr = new int[(groups-1)*2];
	        int k = 0;
	        for (Integer i: hs.keySet()){
	            arr[k] = i;
	            arr[k+1] = hs.get(i);
	            k += 2;
	        }
	        Arrays.sort(arr);//升序排列
			 
	        int begin = arr[0];//啓動組編號
	        int end = hs.get(begin);//截至組編號
	        int current;//當前位置
	        String result = "";
	        for (int i=1; i < arr.length; i++){
	            current = arr[i];
	
	            if (hs.get(current) != null){//證明current是一個起始位
	                int comp = hs.get(current);
	                end = comp > end ?comp: end;
	                continue;
	            }else if(current == end){
	
	                if (result != ""){
	                    result += ";";
	                }
	                result += begin + "," + end;
	
	                if (i+1 < arr.length) {
	                    begin = arr[i + 1];
	                    end = hs.get(begin);
	                }
	            }
	
	
	        }
	
	        System.out.println(result);
	
	    }
	
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章