1.
CAT TAB BADC
true
2.
CAT TAB
false
import java.util.HashMap;import java.util.Map;import java.util.Scanner;publicclassTest2{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);String line = scanner.nextLine();String[] lineSplit = line.split(" ");Map<Character, Integer> map =newHashMap<>();int i =0;for(String s : lineSplit){map.put(s.charAt(0), map.getOrDefault(s.charAt(0),0)+1);map.put(s.charAt(s.length()-1), map.getOrDefault(s.charAt(s.length()-1),0)+1);}boolean res =true;for(Map.Entry<Character, Integer> entry : map.entrySet()){if(entry.getValue()%2==1){res =false;}}System.out.println(res);}}
三(100)
單線程完成任務(wù),要求平均時(shí)長最短,返回時(shí)長為完成任務(wù)時(shí)刻-接收任務(wù)時(shí)刻。假設(shè),零時(shí),接收全部任務(wù)。 任務(wù)有依賴關(guān)系。 輸入: N M n個(gè)任務(wù)耗費(fèi),m個(gè)依賴關(guān)系 X X X… N個(gè)耗費(fèi) X Y Y依賴于X, X需要先完成 輸出: 任務(wù)完成序列 (字典序最小)
示例輸入:
5 6
1 2 1 1 1
1 2
1 3
1 4
2 5
3 5
4 5
import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.Scanner;publicclassTest3{publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);int n = sc.nextInt();// n個(gè)節(jié)點(diǎn)int m = sc.nextInt();// m個(gè)關(guān)系sc.nextLine();Node[] nodes =newNode[n];for(int i =0; i < n; i++){nodes[i]=newNode();nodes[i].cost = sc.nextInt();nodes[i].index = i +1;// 從1開始}sc.nextLine();// 構(gòu)建圖for(int i =0; i < m; i++){int from = sc.nextInt()-1;int to = sc.nextInt()-1;sc.nextLine();nodes[from].nexts.add(nodes[to]);nodes[to].rudu++;}int deleteCount =0;List<Integer> res =newArrayList<>();while(deleteCount < n){boolean isHaveRudu0 =false;ArrayList<Node> deleteNodeSet =newArrayList<>();for(int i =0; i < n; i++){Node node = nodes[i];if(node != null){if(node.rudu ==0){isHaveRudu0 =true;deleteNodeSet.add(node);}}}deleteNodeSet.sort(newComparator<Node>(){@Overridepublicintcompare(Node o1, Node o2){int res1 = Integer.compare(o1.cost, o2.cost);if(res1 ==0){return Integer.compare(o1.index, o2.index);}return res1;}});if(isHaveRudu0){Node node = deleteNodeSet.get(0);// 刪除nodes[node.index -1]= null;res.add(node.index);deleteCount++;for(Node nn : node.nexts){nn.rudu--;}}else{return;}}for(int i =0; i < res.size()-1; i++){System.out.print(res.get(i)+" ");}System.out.println(res.get(res.size()-1));}staticclassNode{int cost =0;int rudu =0;int index =-1;List<Node> nexts =newArrayList<>();}}
四(5)
多多雞寶寶在玩搭積木游戲。有N個(gè)長方體積木,每個(gè)積木的高都是 1,長寬都為Li ,重量為 Wi。 現(xiàn)在多多雞寶寶想要用這些積木搭一個(gè)高高的金字塔。他打算金字塔的每 一層是由且僅由一塊積木組成,同時(shí)每一層的積木邊長都嚴(yán)格比在其下方 的積木小。 在多次嘗試之后,多多雞寶寶發(fā)現(xiàn)每塊積木只能承受自身重量的7倍重量 一若超過7倍自重,搭建的金字塔會因此變得不穩(wěn)定。具體來說即:對于 每一塊積木,在其上方的積木重量之和必須小于等于其自重的7倍。 多多雞寶寶想請你幫他計(jì)算一下最高可以搭一個(gè)多高的金字塔? 數(shù)據(jù)范圍: 1 <= N <= 100 1 <= Li <= 1000 1 <= Wi <= 1000