科大訊飛2017秋招筆試程式設計題
阿新 • • 發佈:2018-12-12
題目描述:隊友爭吵
思路:
感覺像是找規律題:因為最左的一個LL、最右的一個R永遠不會被消去,所以我們需要做的就是最後一個L的位置減去第一個R的位置就是消去的人數。
程式碼實現:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String queue = sc.next();
int lCount = 0 , rCount = queue.length() - 1, count = 0;
for (int i = 0; i < queue.length(); i++) {
if (queue.charAt(i) == 'R')//找到第一個R
break;
lCount++;
}
for (int j = queue.length() - 1; j >= 0; j--) {
if (queue.charAt(j) == 'L')//找到最後一個L
break ;
rCount--;
}
System.out.println(lCount + " " + rCount);
count = queue.length() - (rCount - lCount);
if (count > queue.length())
count = queue.length();
System.out.println(count);
}
}
或者一行實現:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String queue = sc.next();
int count = 0;
count = queue.length() - (queue.lastIndexOf("L") - queue.indexOf("R"));
System.out.println(count);
}
}
題目描述:球賽
思路:
整體來說,可能大家思路都比較清晰,麻煩的就是輸入,然後對輸入的拆分處理是關鍵。
尤其注意最終排名的要求:首先按積分,積分相同按淨勝球,再相同按進球數。所以在寫排序函式的時候需要注意。
程式碼實現:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeMap;
public class laoxu {
class Team {
String name;
int grade;
int realwin;
int in;
public Team(String name, int grade, int realwin, int in) {
this.name = name;
this.grade = grade;
this.realwin = realwin;
this.in = in;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void setRealwin(int realwin) {
this.realwin = realwin;
}
public void setIn(int in) {
this.in = in;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
laoxu main = new laoxu();
while (sc.hasNext()) {
String ns = sc.nextLine();
int n = Integer.valueOf(ns);
Team[] win = new Team[n];// n支球隊的名字
TreeMap<String, Team> map = new TreeMap<>();
for (int i = 0; i < n; i++) {
String cname = sc.nextLine();
Team team = main.new Team(cname, 0, 0, 0);
map.put(cname, team);
}
int len = (n * (n - 1)) / 2;// 對所有的比賽結果進行處理
for (int i = 0; i < len; i++) {
// 以空格分為對戰球隊str[0]和比分str[1]分別進行處理
// A-B 1:2
String[] str = sc.nextLine().split(" ");
String[] name = str[0].split("-");
String cname1 = name[0];// A
String cname2 = name[1];// B
String[] grad = str[1].split(":");
int c1 = Integer.valueOf(grad[0]);// 1
int c2 = Integer.valueOf(grad[1]);// 2
int c = c1 - c2;// 比分差距,用於計算淨勝球
int grade1 = 0;// 得分 3 1 0
int realwin1 = 0;// 淨勝球數
int in1 = c1;// 進球數
int grade2 = 0;
int realwin2 = 0;
int in2 = c2;
if (c == 0) {// 平局
grade1 = 1;
grade2 = 1;
}
if (c > 0) {
grade1 = 3;
realwin1 = c;
}
if (c < 0) {
grade2 = 3;
realwin2 = c2 - c1;
}
//設定得分、淨勝球數、進球數
Team team = map.get(cname1);
team.setGrade(team.grade + grade1);
team.setIn(team.in + in1);
team.setRealwin(team.realwin + realwin1);
map.put(cname1, team);
Team team2 = map.get(cname2);
team2.setGrade(team2.grade + grade2);
team2.setIn(team2.in + in2);
team2.setRealwin(team2.realwin + realwin2);
map.put(cname2, team2);
}
int i = 0;
for (String key : map.keySet()) {
win[i++] = map.get(key);
}
//自定義排序比較器。排序規則:
//首先按照積分,其次淨勝球,最後進球數
Arrays.sort(win, new Comparator<Team>() {
public int compare(Team o1, Team o2) {
int grade = o2.grade - o1.grade;
int realwin = o2.realwin - o1.realwin;
int in = o2.in - o1.in;
if (grade != 0)
return grade;
else if (realwin != 0)
return realwin;
else
return in;
}
});
String[] name = new String[n / 2];
for (i = 0; i < n / 2; i++)
name[i] = win[i].name;
Arrays.sort(name);//對球隊名字按字典序輸出
for (i = 0; i < n / 2; i++)
System.out.println(name[i]);
}
}
}
題目描述:課程衝突
思路:
很簡單,利用map的鍵值對特性,鍵儲存時間資訊,值儲存對應時間的課程資訊;因為考慮到同一時間可能有多門課程(即衝突)的情況,所以值用ArrayList來儲存。
處理完後,遍歷map,找出相應時間的list的大小(也就是同一時間有幾門課),如果大於1,說明有衝突;如果都沒有,則輸出“YES”。
但要注意題目要求按時間順序輸出,所以就需要使用TreeMap了。
程式碼實現:
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, List<String>> map = new TreeMap<String, List<String>>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String time = sc.next(), code = sc.next();
if (!map.containsKey(time))
map.put(time, new ArrayList<String>());
map.get(time).add(code);
}
for (String time : map.keySet()) {
if (map.get(time).size() > 1) {
System.out.printf(time);
for (String code : map.get(time))
System.out.printf(" " + code);
System.out.printf("\n");
}
}
}
}
—–樂於分享,共同進步,歡迎補充
—–Any comments greatly appreciated
—–誠心歡迎各位交流討論!QQ:1138517609