3.8
题目1:数的分解
枚举
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 boolean check(int x) {
while (x > 0) {
int mod = x % 10;
if (mod == 2 || mod == 4) return false;
x /= 10;
}
return true;
}
public static void main(String[] args) throws Exception {
// int cnt = 0;
// for (int i = 1; i < 2019; i++) {
// if (!check(i)) continue;
// for (int j = i + 1; j < 2019; j++) {
// if (!check(j)) continue;
// for (int k = j + 1; k < 2019; k++) {
// if (check(k) && i + j + k == 2019) cnt++;
// }
// }
// }
// out.println(cnt);
out.println(40785);
out.flush();
in.close();
}
}
题目2:猜生日 枚举
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 boolean check(int i, int j) {
int num = i * 10000 + 600 + j;
return num % 2012 == 0 && num % 3 == 0 && num % 12 == 0;
}
public static void main(String[] args) throws Exception {
// for (int i = 2011; i >= 1900; i--) {
// for (int j = 1; j <= 30; j++) {
// if (check(i, j)) {
// out.println(i + " " + j);
// break;
// }
// }
// }
out.println("19550604");
out.flush();
in.close();
}
}
题目3:成绩分析
模拟
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 n = Integer.parseInt(in.readLine());
double cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
int t = Integer.parseInt(in.readLine());
if (t >= 60) cnt1++;
if (t >= 85) cnt2++;
}
out.println(Math.round((cnt1 / n * 100)) + "%");
out.println(Math.round((cnt2 / n * 100)) + "%");
out.flush();
in.close();
}
}
题目4:最大和
dp,题目已经给了状态转移方程了
f[i] 为从起点出发到点 i 的最大价值,从可以一步到达 i 点的所有点中找到价值最大的即可
f[i] = max(f[i], f[j] + v[i])
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)1e4 + 10, n;
static int[] a = new int[N], f = new int[N];
public static int getDn(int x) {
x = n - x;
if (x == 1) return 1;
int p = 2;
while (p * p <= x) {
while (x % p == 0) {
return p;
}
p++;
}
return x;
}
public static void main(String[] args) throws Exception {
n = Integer.parseInt(in.readLine());
String[] s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
}
Arrays.fill(f, -0x3f3f3f3f);
f[1] = a[1];
for (int i = 1; i <= n; i++) {
int st = i + 1, ed = Math.min(n, getDn(i) + i);
for (int j = st; j <= ed; j++) f[j] = Math.max(f[j], f[i] + a[j]);
}
out.println(f[n]);
out.flush();
in.close();
}
}