目录
一、编程题
1.美国节日
链接:美国节日__牛客网 (nowcoder.com)
🔎思路分析:
✨按照这个思路,我们如何让得知某个年月日是星期几:
🌈此时把问题转化为求解:中间经历多少闰年 和 最后一年的天数
1️⃣以上解析实际上是求出 给定y、m、d,如何得知该天是星球几
2️⃣已知星球几,求具体是哪天:
总结:
public class Main {
private static boolean isLeapYear(int y) {
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
private static final int[] DAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 给定 y,m,d,返回这年过了多少天了
private static int nDays(int y, int m, int d) {
// m: 12
// [0, 10]
int n = d;
for (int i = 0; i < m - 1; i++) {
n += DAYS[i];
}
if (m > 2 && isLeapYear(y)) {
n++;
}
return n;
}
// 传入 y,m,d,找到从公元前 1 年12月31日开始过了多久了。求出它的 MOD 7 的同余数
private static int diff(int y, int m, int d) {
return (y - 1) + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + nDays(y, m, d);
}
// 根据 y,m,d 求出星期几
private static int week(int y, int m, int d) {
int w = diff(y, m, d) % 7;
if (w == 0) {
w = 7;
}
return w;
}
// 根据 1 日星期 w,求第 n 个星期 e 是几号
private static int m1(int w, int n, int e) {
return 1 + (n - 1) * 7 + (7 - w + e) % 7;
}
// 根据 6月1日星期 w,求5月的最后一个星期一
private static int m2(int w) {
int d = (w == 1 ? 7 : w - 1);
return 32 - d;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNextInt()) {
int y = s.nextInt();
System.out.printf("%d-01-01\n", y);
int w;
w = week(y, 1, 1);
System.out.printf("%d-01-%02d\n", y, m1(w, 3, 1));
w = week(y, 2, 1);
System.out.printf("%d-02-%02d\n", y, m1(w, 3, 1));
w = week(y, 6, 1);
System.out.printf("%d-05-%02d\n", y, m2(w));
System.out.printf("%d-07-04\n", y);
w = week(y, 9, 1);
System.out.printf("%d-09-%02d\n", y, m1(w, 1, 1));
w = week(y, 11, 1);
System.out.printf("%d-11-%02d\n", y, m1(w, 4, 4));
System.out.printf("%d-12-25\n", y);
System.out.println();
}
}
}
2.因式分解
链接:分解因数__牛客网 (nowcoder.com)
🔎思路分析:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int a = scanner.nextInt();
List<String> factors = factorization(a);
System.out.printf("%d = %s\n", a, String.join(" * ", factors));
}
}
private static List<String> factorization(int a) {
List<String> ans = new ArrayList<>();
for (int i = 2; a > 1 && i * i <= a; i++) {
while (a % i == 0) {
ans.add(String.valueOf(i));
a = a / i;
}
}
if (a > 1) {
ans.add(String.valueOf(a));
}
return ans;
}
}