第一题:P1049 [NOIP2001 普及组] 装箱问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
package 蓝桥算法训练__普及组.Day3;
import java.util.Scanner;
public class T1 {
static int[] w = new int[31];
static int[] dp = new int[20010];
static int v, n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
v = sc.nextInt();
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
w[i] = sc.nextInt();
}
for (int i = 1; i <= n; i++) {
for (int j = v; j <= w[i]; j++) {
dp[j] = Math.max(dp[j], dp[j - w[i]] + w[i]);
}
}
System.out.println(v-dp[v]);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/4b71a1fce11d0bbe81e86d7da53e0236.png)
第二题:P8647 [蓝桥杯 2017 省 AB] 分巧克力 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
package 蓝桥算法训练__普及组.Day3;
import java.util.Scanner;
public class T2 {
static int n,k;// n块巧克力,k个小朋友
static int N = 100100;
static int[] h = new int[N];
static int[] w = new int[N];
// 边长为x的正方形巧克力满足的孩子的个数
static int check(int x) {
int s = 0;
for (int i = 0; i < n; i++) {
s += (h[i] / x) * (w[i] / x);
}
return s;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for (int i = 0; i < n; i++) {
h[i] = sc.nextInt();
w[i] = sc.nextInt();
}
// 二分
int left = 0;
int right = 100000;
while (left < right) {
int mid = (left + right + 1) >> 1;
// check(mid)是边长为mid值的巧克力能分给的孩子的个数
// 若 >= k 则答案在mid或者mid的右边
if (check(mid) >= k) {
left = mid;
} else {
// 反之在 mid 的左边
right = mid - 1;
}
}
System.out.println(left);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/71fa87995204b31f44c56996fd557316.png)
第三题:P1824 进击的奶牛 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
package 蓝桥算法训练__普及组.Day3;
import java.util.Arrays;
import java.util.Scanner;
public class T3 {
static int n,c;
static int[] a;
// 距离为x时 所包含的牛的数目
static int check(int x) {
int k = 1;
int count = 1;
for (int i = 2; i <= n; i++) {
if (a[i] - a[k] >= x) {
count++;
k = i;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
c = sc.nextInt();
a = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
//二分
int left = 0;
int right = a[n] - a[1];
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(mid) >= c) {
left = mid;
} else {
right = mid - 1;
}
}
System.out.println(left);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/ec1d15157aadcbbbc85da7e992387da8.png)
第四题:P1036 [NOIP2002 普及组] 选数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
package 蓝桥算法训练__普及组.Day3;
import java.util.Scanner;
public class T4 {
static boolean isPrime(int x) {
for (int i = 2; i*i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
//k表示第几个数 nums表示加到第几个数了 sum表示此时的和值
static void dfs(int k, int nums, int sum) {
if (nums == m) {
if (isPrime(sum)) {
ans++;
}
} else if (k <= n) {
dfs(k+1,nums,sum);
dfs(k+1,nums+1,sum+a[k]);
}
}
static int n,m,ans,sum;
static int[] a = new int[21];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
dfs(1,0,0);
System.out.println(ans);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e891622923d2c30dd96219bef1f1da40.png)