Java在算法竞赛中的技巧(蓝桥杯备赛总结)

前言:笔者在这段时间准备蓝桥杯竞赛,由于个人原因选择Java作为语言,刷题中也是不断感到Java有些语法还是不够方便(非常羡慕隔壁C++的STL…),不过有些常见的技巧/方法/模板,也是自己做了些总结,十分之不全面,比完赛会继续完善…

!!!!!提交结果时记得检查有无不该加的头文件,主类名是否为Main!!!!!!

2.优化输入输出时间(快速IO模板):

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main {
   
		static InputReader in = new InputReader();
		static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	public static void main(String[] args) throws Exception {
   
		/**
			你的代码写在这里
			(输入实例: int a = in.nextInt();)
		*/
		out.close(); //不关闭输出流的话,控制台会没有输出,所以一定要关,in最好也关,不过实测没有因为不关in出过问题
	}
	
	static class InputReader{
   
		private StringTokenizer st;
		private BufferedReader bf;
		
		public InputReader() {
   
			bf = new BufferedReader(new InputStreamReader(System.in));
			st = null;
		}
		
		public String next() throws IOException{
   
			while(st == null || !st.hasMoreTokens()) {
   
				st = new StringTokenizer(bf.readLine());
			}
			return st.nextToken();
		}
		
		public String nextLine() throws IOException{
   
			return bf.readLine();
		}

		public int nextInt() throws IOException{
   
			return Integer.parseInt(next());
		}
		
		public long nextLong() throws IOException{
   
			return Long.parseLong(next());
		}
		
		public double nextDouble() throws IOException{
   
			return Double.parseDouble(next());
		}
		
		public BigInteger nextBigInteger() throws IOException{
   
			return new BigInteger(next());
		}
		
		public BigDecimal nextBigDecimal() throws IOException{
   
			return new BigDecimal(next());
		}
	} 
} 

3.快速幂模板

long FastPower(long base, long power) {
        //base是底数,power是幂数,result是结果
    long result= 1;
    while(power > 0) {
   
	if((power & 1) != 0) {
   
	    result*= base;
	    power -= 1;
	}
	base *= base;
	power >>= 1;
    }
    return result;
}

4.自定义类排序
例如:

public class Main {
   
	static class Point{
   
    	double x;
    	double y;
    	public Point() {
   }
    	public Point(double x, double y){
   
    		this.x = x;
    		this.y = y;
    	}
    }
	public static double INF = 2 << 19;
    static InputReader in;  
    static PrintWriter out;  
    public static int n;
    public static double dist(Point a, Point b) {
   
    	double aa = Math.pow(a.x - b.x, 2);
    	double bb = Math.pow(a.y - b.y, 2);
    	return Math.sqrt(aa + bb);
    }
    public static double merge(Point[] point, int left, int right) {
   
    	double d = INF;
    	if(left >= right)
    		return d;
    	if(left + 1 == right)
    		return dist(point[left], point[right]);
    	int mid = (left + right) >> 1;
    	double d1 = merge(point, left, mid);
    	double d2 = merge(point, mid+1, right);
    	d = Math.min(d1, d2);
    	int i, j, k = 0;
    	ArrayList<Point> tem = new ArrayList<>();
    	for(i = left; i <= right; ++i) {
   
    		if(Math.abs(point[mid].x - point[i].x) <= d) {
   
    			tem.add(point[i]);
    			k++;
    		}
    	}
    	Collections.sort(tem, new Comparator<Point>() {
   
    		@Override
    		public final int compare(Point pFirst, Point pSecond) {
   
    			if(pFirst.y < pSecond.y) 
    				return -1;
    			if(pFirst.y > pSecond.y)
    				return 1;
    			if(pFirst.x < pSecond.x)
    				return -1;
    			if(pFirst.x > pSecond.x)
    				return 1;
    			return 0;
    		}
		});
    	for(i = 0; i < k; i++) {
   
    		for(j = i + 1; j < k && (tem.get(j).y - tem.get(i).y) < d; j++) {
   
    			double d3 = dist(tem.get(j), tem.get(i));
    			if(d3 < d) d 
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值