特殊日期
原题链接
问题描述
解题思路
模拟即可
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static boolean isLeap(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
public static int check(int x) {
int ans = 0;
while (x > 0) {
ans += x % 10;
x /= 10;
}
return ans;
}
public static void main(String[] args) throws Exception {
int ans = 0;
for (int i = 1900; i <= 9999; i++) {
days[2] = isLeap(i)? 29: 28;
for (int j = 1; j <= 12; j++) {
for (int k = 1; k <= days[j]; k++) {
if (check(i) == check(j) + check(k)) ans++;
}
}
}
out.println(ans);
out.flush();
in.close();
}
}
重合次数
原题链接
问题描述
解题思路
模拟即可,每 60 秒就会重合一次。注意这个题有个坑,所以就是 60 分钟重合 59 次,折合一下就是每 61 秒重合一次。
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
int st = 6 * 60 * 60 + 13 * 60 + 22;
int ed = 14 * 60 * 60 + 36 * 60 + 20;
out.println((ed - st) / 61);
out.flush();
in.close();
}
}
左移右移
原题链接
问题描述
解题思路
每个数的初始权重是它本身,如果移动到最左端,那么它的权重就会变成最小值,最右端同理,最后根据权重排序即可。
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)2e5 + 10, n, m;
static int[][] a = new int[N][2];
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
for (int i = 1; i <= n; i++) {
a[i][1] = i;
a[i][0] = i;
}
int st = 0, ed = n + 1;
for (int i = 0; i < m; i++) {
s = in.readLine().split(" ");
int t = Integer.parseInt(s[1]);
if (s[0].equals("L")) {
a[t][0] = st--;
} else {
a[t][0] = ed++;
}
}
Arrays.sort(a, 1, n + 1, (a, b) -> (a[0] - b[0]));
for (int i = 1; i <= n; i++) out.print(a[i][1] + " ");
out.flush();
in.close();
}
}
近似gcd
原题链接
问题描述
解题思路
梗子哥这篇题解写的非常的清楚了近似GCD
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, n, g;
static int[] a = new int[N];
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
g = Integer.parseInt(s[1]);
s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]) % g == 0? 1: 0;
a[i] += a[i - 1];
}
long ans = 0L;
int start = 0;
for (int end = 2; end <= n; end++) {
while (start + 1 < end && a[end] - a[start] + 1 < end - start) start++;
ans += end - start - 1;
}
out.println(ans);
out.flush();
in.close();
}
}