提交 5e322493 编写于 作者: qq_36480062's avatar qq_36480062

commit

上级 2f99266f
......@@ -42,5 +42,4 @@ public class HDOJ阶乘2940 {
}
}
}
}
package UnionFind;
//最优实现并查集
public class UnionFind {
public int[] parent;
private int[] rank;
UnionFind(int n) {
parent = new int[n+1];
rank = new int[n+1];
for (int i = 0; i < n+1; i++) {
parent = new int[n + 1];
rank = new int[n + 1];
for (int i = 0; i < n + 1; i++) {
parent[i] = i;
rank[i] = 1;
}
......
package Water;
public class 最大最小公倍数 {
public static void main(String[] args) {
}
}
package Water;
import java.util.Scanner;
/**
* http://lx.lanqiao.cn/problem.page?gpid=T10
* 问题描述
* 杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
*
*
* 它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
*
*
* 下面给出了杨辉三角形的前4行:
*
*
* 1
*
*
* 1 1
*
*
* 1 2 1
*
*
* 1 3 3 1
*
*
* 给出n,输出它的前n行。
*
* 输入格式
* 输入包含一个数n。
*
* 输出格式
* 输出杨辉三角形的前n行。每一行从这一行的第一个数开始依次输出,中间使用一个空格分隔。请不要在前面输出多余的空格。
* 样例输入
* 4
* 样例输出
* 1
* 1 1
* 1 2 1
* 1 3 3 1
* 数据规模与约定
* 1 <= n <= 34。
*/
public class 杨辉三角形 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] dp = new int[n][n];
dp[0][0] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
if (j - 1 >= 0)
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
else
dp[i][j] = dp[i - 1][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
}
}
package Water;
import java.math.BigInteger;
import java.util.Scanner;
public class 阶乘 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nextInt = sc.nextInt();
BigInteger b = BigInteger.ONE;
for (long i = 1; i <= nextInt; i++) {
b = b.multiply(BigInteger.valueOf(i));
}
System.out.println(s(b.toString()));
}
public static char s(String n) {
for (int i = n.length() - 1; i >= 0; i--) {
if (n.charAt(i) != '0')
return n.charAt(i);
}
return 0;
}
}
package com.shiyu;
import java.util.*;
import java.util.Scanner;
public class Main {
private static class Node implements Comparable<Node> {
public int Juli;
public int You = 0;
public Node(int juli) {
Juli = juli;
}
@Override
public int compareTo(Node node) {
return this.Juli - node.Juli;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();//几个加油站
Node[] node = new Node[N + 1];
for (int i = 0; i < N; i++) {
node[i] = new Node(sc.nextInt());
node[i].You = sc.nextInt();
}
int L = sc.nextInt();
int P = sc.nextInt();
for (int i = 0; i < N; i++) {
node[i].Juli = L - node[i].Juli;//更改为加油站距离起点的位置
}
node[N] = new Node(L);
Arrays.sort(node);
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(N + 10, new Comparator<Integer>() {
@Override
public int compare(Integer t1, Integer t2) {
return t2 - t1;
}
});
int res = 0;//加油次数
int pos = 0;//当前所在位置
int tank = P;//油箱剩的油的数量
for (int i = 0; i <= N; i++) {//把终点看做加油站,看能不能到这个加油站
int d = node[i].Juli - pos;//卡车的位置从0开始,到达第i个加油站需要多少油
while (tank - d < 0) {//如果无法到达第i个加油站
if (pq.size() == 0)//从优先队列里面没有加油站,没有刻
System.out.println("-1");
tank += pq.poll();
res++;
}
tank -= d;//减去走到第i个加油站需要的油,区间是什么呢,
// 第一次区间(0,A[i])需要这么多油,第二次及以后区间(A[i-1],A[i])
// 每次都要维护油的数量,区间消耗的油
pos = node[i].Juli;//卡车开到第i个加油站
pq.offer(node[i].You);//每到一个加油站,就把能加的油加进优先队列
}
System.out.println(res);
String next = sc.next();
System.out.println(next.toLowerCase());
}
}
}
\ No newline at end of file
......@@ -2,6 +2,10 @@ package com.shiyu;
public class tsdt {
public static void main(String[] args) {
System.out.println(153);
System.out.println(370);
System.out.println(371);
System.out.println(407);
}
......
......@@ -31,4 +31,4 @@ public class 开灯 {
System.out.print(i + " ");
}
}
}
}
\ No newline at end of file
package string;
import java.util.Arrays;
import java.util.Random;
/**
* KMP字符串匹配...未完
*/
public class KMP {
public static void main(String[] args) {
for (int i = 3; i < 1000; i++) {
String st = randomStr(i);
if (!Arrays.equals(next(st), nextt(st))) {
System.out.println(st);
System.out.println(Arrays.toString(next(st)));
System.out.println(Arrays.toString(nextt(st)));
}
}
System.out.println(Arrays.toString(next("abaabcac")));
System.out.println(Arrays.toString(nextt("abaabcac")));
}
public static String randomStr(int len) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < len; i++) {
Random r = new Random();
char random = (char) (r.nextInt(26) + 'a');
s.append(random);
}
return s.toString();
}
......@@ -29,6 +53,13 @@ public class KMP {
return -1;
}
/**
* 前缀 指除了最后一个字符以外,一个字符串的全部头部组合;
* 后缀 指除了第一个字符以外,一个字符串的全部尾部组合。
*
* @param ps 模式串
* @return 求解next数组
*/
public static int[] next(String ps) {
int pLength = ps.length();
int[] next = new int[pLength];
......@@ -38,6 +69,7 @@ public class KMP {
return next;
}
next[1] = 0;
int j = 1;
int k = next[j];//看看j的最长匹配前缀在哪里
while (j < pLength - 1) {
......@@ -48,8 +80,21 @@ public class KMP {
return next;
}
public static void m() {
public static int[] nextt(String ps) {
int pLength = ps.length();
int[] next = new int[pLength];
char[] p = ps.toCharArray();
next[0] = -1;
if (ps.length() == 1) {
return next;
}
int j = 0, k = -1;
while (j < ps.length() - 1) {
if (k == -1 || p[j] == p[k]) {
next[++j] = ++k;
} else k = next[k];//此语句是这段代码最反人类的地方,如果你一下子就能看懂,那么请允许我称呼你一声大神!
}
return next;
}
}
......@@ -59,6 +59,4 @@ public class zhuanhuan {
}
return count;
}
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ package 进制转换;
public class 进制转换 {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(123));
System.out.println(change(123,2));
System.out.println(change(123, 2));
}
public static char[] arr = {'a', 'b'};
......@@ -41,4 +41,43 @@ public class 进制转换 {
}
}
}
/**
* @param n 需要转的10进制数字
*/
public static void Ten转16进制(int n) {
char[] arr = new char[2576];
int cur = n;
int count = 0;
while (cur != 0) {
if (cur % 16 >= 10)
arr[count++] = (char) (cur % 16 - 10 + 'A');
else
arr[count++] = (char) (cur % 16 + '0');
cur /= 16;
}
StringBuilder sb = new StringBuilder();
for (int i = count - 1; i >= 0; i--) {
sb.append(arr[i]);
}
System.out.println(sb);
}
/**
* @param n 需要转的16进制数
*/
public static void Sixto10进制(String n) {
long res = 0;
int t = 0;
int c = 0;
for (int i = n.length() - 1; i >= 0; i--) {
if (n.charAt(i) >= 'A') {
t = n.charAt(i) - 'A' + 10;
} else {
t = n.charAt(i) - '0';
}
res += t * Math.pow(16, c++);
}
System.out.println(res);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册