提交 09eae3d2 编写于 作者: qq_36480062's avatar qq_36480062

commit

上级 5d3b252a
public class _152乘积最大子序列 {
}
......@@ -8,12 +8,15 @@ public class 埃氏筛法 {
long l = System.nanoTime();
System.out.println(AIshi(1234341));
long t = System.nanoTime();
System.out.println(t-l);
System.out.println(t - l);
l = System.nanoTime();
eluer(1234341);
t = System.nanoTime();
System.out.println(t-l);
System.out.println(t - l);
l = System.nanoTime();
System.out.println(Bei(1234341));
t = System.nanoTime();
System.out.println(t - l);
}
/**
......@@ -58,7 +61,7 @@ public class 埃氏筛法 {
static int[] visited;
/**
* 欧拉线性筛法求第N个质数
* 欧拉线性筛法求第N个质数(最快的!)
*
* @param N
*/
......@@ -81,4 +84,26 @@ public class 埃氏筛法 {
}
System.out.println(prime[N]);
}
static int Bei(int n) {
int c = 1000;
while ((c / Math.log(c)) < n) {
c++;
}//判断第n个数的范围
boolean[] visited = new boolean[c];
visited[1] = true;
for (int i = 2; i < c; i++) {
if (!visited[2]) {
for (int j = 2 * i; j < c; j += i) {
visited[j] = true;
}
}
}
int ans = 0;
for (int i = 2; i < c; i++) {
if (!visited[i]) ans++;
if (ans == n) return i;
}
return -1;
}
}
package Math;
public class 欧拉线性筛最快的 {
public static void main(String[] args) {
eluer(12345);
}
static int[] prime;
static int[] visited;
/**
* 欧拉线性筛法求第N个质数(最快的!)
*
* @param N
*/
static void eluer(int N) {
int n = 1000;
while ((n / Math.log(n)) < N) {
n++;
}
prime = new int[n + 1];
visited = new int[n + 1];
for (int i = 2; i <= n; i++) {
if (visited[i] == 0) {
prime[++prime[0]] = i;//这个prime[0] 相当于 cnt,用来计数
}
for (int j = 1; j <= prime[0] && i * prime[j] <= n; j++) {
visited[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
System.out.println(prime[N]);
}
}
package Math;
import java.util.Scanner;
import static java.lang.System.in;
public class 龟兔赛跑 {
public static void main(String[] args) {
Scanner sc = new Scanner(in);
int v1 = sc.nextInt();
int v2 = sc.nextInt();
int cha = v1 - v2;
int t = sc.nextInt();
int s = sc.nextInt();
int l = sc.nextInt();
int wugui=l/v2;
}
}
......@@ -137,7 +137,6 @@ public class LIS {
* 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
* 你算法的时间复杂度应该为 O(n2) 。
* 进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?
* <p>
* 思路与上一题类似:
* dp数组语义,dp[i] 表示以位置 i 结尾的子序列的最大长度
*
......
###动态规划(打表)自底向上
本质是暴力加上记忆化
```
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
......
......@@ -33,7 +33,6 @@ import java.util.Scanner;
* BFS会把状态逐个加入队列,因此通常需要与状态数成正比的内存空间,
* 反之,DFS是与最大递归深度成正比的,一般与状态数相比,递归的深度并不会太大
* 一般来说DFS比BFS更节省内存,
*
*/
public class 迷宫最短路径BFS实现 {
......
......@@ -8,7 +8,17 @@ package 尺取法;
* 区间内所有数的和 sum >= 15
* 求出满足这样要求的最小的区间长度
* The truth that you leave
* 双指针
* 双指针类似吧
* 尺取法,通常用于区间有一定规律,或者说有一定变化趋势
* 对所选取区间进行判断之后,可以推出满足条件的区间,
* 如果判断目前所选区间,但无法确定所求解的区间,如何进一步推出,无法使用尺取法
*
* 尺取法的模型便是这样
* 根据区间的特征交替推进左右端点求解问题
* 其高效的原因在于避免了大量的无效枚举
* 其区间枚举都是根据区间特征有方向的枚举
* 如果胡乱使用尺取法的话会使得枚举量减少
* 因而很大可能会错误所以关键的一步是进行问题的分析
*/
@SuppressWarnings("all")
public class ruler {
......@@ -21,6 +31,7 @@ public class ruler {
}
/**
* Poj 3061
* 给你一个序列(大小为n),让你去找到一个区间[i,j],
* 区间内所有数的和 sum >= 15
* 求出满足这样要求的最小的区间长度
......
package 第七章枚举;
import java.util.Arrays;
import java.util.HashSet;
/**
* l7.1除法( Division, UVa 725)
* 输入正整数n, 按从小到大的顺序输出所有形如abcde/fghij = n的表达式, 其中a~ j恰好
* 为数字0~ 9的一个排列( 可以有前导0) , 2≤n≤79。
* 样例输入:
* 62
* 样例输出:
* 79546 / 01283 = 62
* 94736 / 01528 = 62
* 【分析】
* 枚举0~ 9的所有排列? 没这个必要。 只需要枚举fghij就可以算出abcde, 然后判断是否
* 所有数字都不相同即可。 不仅程序简单, 而且枚举量也从10!=3628800降低至不到1万, 而且
* 当abcde和fghij加起来超过10位时可以终止枚举。 由此可见, 即使采用暴力枚举, 也是需要认
* 真分析问题的。
*/
public class l1除法 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
f(62);
}
static int[] arr = new int[10];
static HashSet<Integer> set = new HashSet<Integer>();
//全排列做法
static void dfs(int[] arr, int k, int N) {
if (k == 10) {
int t = arr[0] * 10000 + arr[1] * 1000 + arr[2] * 100 + arr[3] * 10 + arr[4];
int s = arr[5] * 10000 + arr[6] * 1000 + arr[7] * 100 + arr[8] * 10 + arr[9];
if (t % s == 0 && t / s == N)
System.out.println(Arrays.toString(arr));
return;
}
for (int i = k; i < 10; i++) {
int t = arr[i];
arr[i] = arr[k];
arr[k] = t;
dfs(arr, k + 1, 62);
t = arr[i];
arr[i] = arr[k];
arr[k] = t;
}
}
//只枚举fghij
static void f(int N) {
for (int i = 1234; i < 100000; i++) {
if (check(i, i * N))
System.out.println(i * N + "/" + i + "=" + N);
}
}
static boolean check(int A, int E) {
if (E >= 100000) return false;
set.clear();
while (A != 0) {
set.add(A % 10);
A /= 10;
}
if (set.size() == 4) set.add(0);
while (E != 0) {
set.add(E % 10);
E /= 10;
}
return set.size() == 10;
}
}
package 第七章枚举;
/**
* 输入n个元素组成的序列S 你需要找出一个乘积最大的连续子序列 如果这个最大的乘
* 积不是正数 应输出0 表示无解 1n18 -10Si10
* 分析
* 连续子序列有两个要素 起点和终点
* 因此只需枚举起点和终点即可.由于每个元素的
* 绝对值不超过10且不超过18个元素
* 最大可能的乘积不会超过10^18 long存储
* 也可使用dp解决参见LeetCode 152乘积最大子序列
*/
public class l2最大连续子序列乘积 {
public static void main(String[] args) {
}
static int f(int[] arr) {
if (arr.length == 1) return Math.max(arr[0], 0);
return 0;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册