1.字符串变数组
static char[][] ch = new char[][] {
"UDDLUULRUL".toCharArray(),
"ULRDLUURRR".toCharArray()};
2.哈希查重
static HashSet<String> set = new HashSet<>();
//...........
String s = ""+x*y;
if(ok(s)) {
String v = x+"x"+y;
String u = y+"x"+x;
if(!set.contains(v)&&!set.contains(u)) {
set.add(u);
set.add(v);
ans++;
3.一些字符串的运用
charAt(i)
b[s.charAt(i)-'0']
//charAt(i)取i位置的字符
//-'0' 表示将char型变成字符型
s.indexOf(“0”)
返回字符串0的下标
s.length()
字符串长度
4.二分模板
while(l<=r) {
int mid = l + (r-l)/2;
if(ok(mid)) {
l=mid+1;
ans=mid;
}else
r = mid - 1;
5.int型变String型
String s=String.valueOf(i);
6…
A题:sum必须要用long型,如果用int会溢出,打印出的是-1636549443
B题:由与题目的要求我们只需求得最后四位,所以模10000即可 不模的话很快就会溢出
7.类的使用
class shop
{
int time[];
public shop (int t)
{
time=new int [t+1];
}
}
//.....
for(int i=0;i<n;i++) {
s[i]=new shop(t);
}
for(int i=0;i<m;i++)
{
temp=in.nextInt();
sh=in.nextInt();
s[sh-1].time[temp]++;
}
8.读取一行字符
for (int i = 0; i < n; i++) {
String line = in.nextLine();
mapp[i] = line.toCharArray();
}
9…
1.Map接口,HashMap实现,<k,v>
新建: Map<String,Boolean> mapp=new HashMap<String,Boolean>();
添加: mapp.put(now.string,true);
查询: mapp.containsKey(node.string)
2.队列, queue接口,LinkedList实现
新建: Queue<Node> queue =new LinkedList<Node>();
进队列: queue.add(now);
出队列: now=queue.poll();
3.字符数组
字符串变数组:char s[]=now.string.toCharArray();
数组变字符串:String str=String.valueOf(s)
10.并查集
package 蓝桥训练系统;
import java.util.Scanner;
public class _54合根植物 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt()*in.nextInt();
m = in.nextInt();
f = new int[n+5];
for(int i=1;i<=n;i++)
f[i] = i;//初始父亲为自己
for(int i=1;i<=m;i++)
merge(in.nextInt(), in.nextInt());
System.out.println(n-cnt);
}
static int[] f;
static int n,m,cnt=0;
static int find(int x) {//找祖先
if(f[x]==x)
return x;
return f[x]=find(f[x]);//路径压缩 //一路所有的结点的父节点都变成最终的跟结点
}
static void merge(int x,int y) {
int a = find(x);
int b = find(y);
if(a!=b) {//祖先不同,合并
f[a] = b;
cnt++;//cnt记录合并祖先次数,合并一次少一根
}
}
}
11.最大公约数
例如:12 15 最大公约数为3 ,最小公倍数为12*15
static int GCD(int x,int y){
if(y==0){
return x;
}
return GCD(y,x%y);//是x%y,不是x/y
}
13.文件输出
System.setOut(new PrintStream(new File("E:\\out3.txt")));
System.out.println(A+""+(B.compareTo(BigInteger.ZERO)<0?"":"+")+B+"i");
14.文件输入
File f = new File("E:\\out3.txt");
Scanner input = new Scanner(f);
while(input.hasNext())
{
String aa = input.next();
}
15.排序
Arrays.sort(n);
“A”的ASCII码值为65;“a”的ASCII码值为97;“0”的ASCII码值为 48。
17.高精度
import java.math.BigInteger;
public class 高精度 {
public static void main(String[] args) {
String temp1 = "-1000000000000000000000000000000000000";
BigInteger bg1 = new BigInteger(temp1); //注意初始化的方式,使用字符串来初始化
System.out.println(bg1.abs()); //绝对值方法 object.abs()
BigInteger bg= new BigInteger (temp1);
String temp2 = "100000000000000000000000000";
BigInteger bg2 = new BigInteger(temp2);
System.out.println(bg1.add(bg2)); //加法 object.add(BigInteger b)
System.out.println(bg1.subtract(bg2)); //减法 返回为 bg1 - bg2 (this - param)
System.out.println(bg1.multiply(bg2)); //乘法 返回 bg1 * bg2
System.out.println(bg1.divide(bg2)); //除法 返回bg1 / bg2
System.out.println(bg1.mod(bg2)); //取模运算 返回的是 bg1%bg2 (this mod param)
System.out.println(bg1.gcd(bg2)); //直接封装好了 求解bg1,bg2 的最大公约数
int temp5 = 5;
System.out.println(bg2.pow(temp5)); //乘方运算 注意这个方法的参数是基本类型int
System.out.println(bg2.compareTo(bg1)); // 比较方法 结果为1 bg2大
System.out.println(bg1.compareTo(bg2)); // 结果为-1 bg2大
//这个地方注意比较的方法,还有一个方法是equal()
String temp3 = "1000";
String temp4 = "001000";
BigInteger bg3 = new BigInteger(temp3);
BigInteger bg4 = new BigInteger(temp4);
System.out.println(bg3.compareTo(bg4)); //结果为0 表示相等
System.out.println(bg3.equals(bg4)); //返回结果为true 这样看是没有区别,但是更推荐比较的时候使用compareTo()方法,
//在BigDecimal更直观,例如0.1 与0.10 ,equal返回false 而compareTo则是正确的结果。
}
}
- 最大值 最小值
int max = Integer.MIN_VALUE,
int min = Integer.MAX_VALUE;
19.以一定格式输出
System.out.println(String.format("%.2f", sum/n));
20.闰年
1.普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)
2.世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
- 【01背包 】/【完全背包】
01背包的状态转移方程为:dp(i,j)=max(dp(i-1,j),dp(i-1,j-v[i])+val[i])
完全背包的状态转移方程:dp(i,j)=max(dp(i-1,j),dp(i,j-v)+val[i])