java算法题目请教,在线等算法高手,400分全散

ChiChengIT 2019-10-24 07:03:32
...全文
209 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
尽然遇到了 就一起刷了吧

public class Test {

/**
* 可取的数量
*/
private static int MAX_LEN = 10;

/**
* 可选择的最大值
*/
private static int MAX_VALUE = 200;

/**
* 预期值
*/
private static int VALUE = 1024;

/**
* 结果
*/
private static long RESULT;

/**
* 储存结果、验证
*/
//private static int[] results = new int[MAX_LEN];

public static void main(String[] args) {
next(0, 0);
System.out.println(RESULT);
}

public static void next(int sum, int currentLen){
if (currentLen + 1 == MAX_LEN) {
if (VALUE - sum > 100) {
RESULT++;
//results[currentLen] = VALUE - sum;
//print();
}
return;
}
for (int i = 100; i < MAX_VALUE; i++) {
//results[currentLen] = i;
long currentResult = RESULT;
next(sum + i, currentLen + 1);
if (currentResult == RESULT) {
return;
}
}
}
/* private static void print(){
for (int result : results) {
System.out.print(result + " ");
}
System.out.println();
}*/

}
wowpH 2019-10-25
  • 打赏
  • 举报
回复
public class Test {
	public int min, max, sum, num;
	public long[][] dp;

	public Test() {
		min = 0;
		max = 24;
		sum = 24;
		num = 10;
		// min = 100;
		// max = 200;
		// sum = 1024;
		// num = 10;
		dp = new long[num + 1][sum + 1];
		for (int i = 0; i <= num; ++i) {
			Arrays.fill(dp[i], 0);
		}
		long ans = solution(num, sum);
		System.out.println(ans);
	}

	private long solution(	int N,
							int S) {
		if (0 == N) {
			if (0 == S) {
				return 1;
			}
			return 0;
		}

		if (S < min * N || S > max * N) {
			return 0;
		}

		if (dp[N][S] > 0) {
			return dp[N][S];
		}

		long count = 0;
		for (int i = min; i <= max; ++i) {
			count += solution(N - 1, S - i);
		}
		dp[N][S] = count;

		return count;
	}

	public static void main(String[] args) {
		new Test();
	}
}
wowpH 2019-10-24
  • 打赏
  • 举报
回复
这不是动态规划吗?

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧