1 Star 2 Fork 2

util6/算法刷题java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
KittenClimbing.java 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
util6 提交于 2022-04-11 19:19 +08:00 . 默认的
package Acwing165小猫爬山;
import java.io.*;
import java.util.Arrays;
public class KittenClimbing {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt() throws Exception{
in.nextToken();
return (int)in.nval;
}
static String next()throws Exception{
in.nextToken();
return in.sval;
}
static final int N = 20;
static int[] a = new int[N], sum = new int[N];
static int n, res = 18, w;
static void dfs(int u, int cnt) {
if (cnt >= res) {
return;
}
if (u >= n) {
res = cnt;
return;
}
for (int i = cnt - 1; i >= 0; i--) {
if (sum[i] + a[u] <= w) {
sum[i] += a[u];
dfs(u + 1, cnt);
sum[i] -= a[u];
}
}
sum[cnt] = a[u];
dfs(u + 1, cnt + 1);
sum[cnt] = 0;
}
public static void main(String[] args)throws Exception {
n = nextInt();
w = nextInt();
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
Arrays.sort(a, 0, n);
dfs(0, 0);
out.println(res);
out.flush();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liuyutaocode/algorithmBrushing.git
git@gitee.com:liuyutaocode/algorithmBrushing.git
liuyutaocode
algorithmBrushing
算法刷题java
master

搜索帮助