同程旅行 0924 筆試:乘法原理 + 模擬
25 道選擇+兩道編程
題1:排列+乘法原理
[1,m]中所有的奇數(shù)可以自由放到 n 數(shù)組中的奇數(shù)位上,同理偶數(shù)自由放到偶數(shù)位上。分別求出兩種方案數(shù)再乘法原理。
import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } int mod = 1000000007; public void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); int oddCnt = m / 2; int evenCnt = m / 2; if(m % 2 != 0) oddCnt++; // m 為奇數(shù)的話,奇數(shù)就多一個 int oddPosCnt = n / 2; // 奇數(shù)的位置個數(shù) int evenPosCnt = n / 2; if(n % 2 != 0) oddPosCnt++; long odd = qmi(oddCnt, oddPosCnt); long even = qmi(evenCnt, evenPosCnt); long ans = (odd * even) % mod; System.out.println(ans); } public long qmi(long a, long b){ long res = 1; while (b > 0) { if((b & 1) == 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } }
題 2:模擬
import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } // 直接模擬判斷就行 public void run() { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); HashSet<String> hasRegistry = new HashSet<>(); // 已經(jīng)注冊過的用戶名 while (t-- > 0) { String s = sc.nextLine(); int n = s.length(); // 如果長度不合法 if (n < 6 || n > 12) { System.out.println("illegal length"); continue; } // 已經(jīng)注冊過 if (hasRegistry.contains(s)) { System.out.println("acount existed"); continue; } boolean flag = false; // 是否遇到數(shù)字 for (int i = 0; i < n; i++) { // 遇到數(shù)字,草率了,遇到非大小寫字母的字符就算 char c = s.charAt(i); if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) { System.out.println("illegal charactor"); flag = true; break; } } // 符合要求 if (!flag) { hasRegistry.add(s); System.out.println("registration complete"); } } } } /** * registration complete * illegal length * acount existed * illegal charactor */#??蛣?chuàng)作賞金賽##軟件開發(fā)筆面經(jīng)##筆試##同程求職進展匯總#
后端開發(fā)筆面經(jīng) 文章被收錄于專欄
主要收錄一部分我的筆試面試經(jīng)歷文章,歡迎訂閱。