同程旅行 0924 笔试:乘法原理 + 模拟
25 道选择+两道编程
题1:排列+乘法原理
[1,m]中所有的奇数可以自由放到 n 数组中的奇数位上,同理偶数自由放到偶数位上。分别求出两种方案数再乘法原理。
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 为奇数的话,奇数就多一个
int oddPosCnt = n / 2; // 奇数的位置个数
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<>(); // 已经注册过的用户名
while (t-- > 0) {
String s = sc.nextLine();
int n = s.length();
// 如果长度不合法
if (n < 6 || n > 12) {
System.out.println("illegal length");
continue;
}
// 已经注册过
if (hasRegistry.contains(s)) {
System.out.println("acount existed");
continue;
}
boolean flag = false; // 是否遇到数字
for (int i = 0; i < n; i++) {
// 遇到数字,草率了,遇到非大小写字母的字符就算
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
*/
#牛客创作赏金赛##软件开发笔面经##笔试##同程求职进展汇总#后端开发笔面经 文章被收录于专栏
主要收录一部分我的笔试面试经历文章,欢迎订阅。

查看6道真题和解析