• Java实现 洛谷 P1582 倒水


    在这里插入图片描述

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
     
    public class Main {
    	static long m = 0, f = 0;
    	public static void main(String[] args) {
    		FastScanner fs = new FastScanner();
    		int a = fs.nextInt();
    		int b = fs.nextInt();
    		long x[] = new long[32];
    		boolean y[] = new boolean[32];
    		int n = 0;
    		x[0] = 1;
    		x[1] = 2;
    		for (int i = 2; i < y.length; i++) {
    			x[i] = 2 * x[i - 1];
    		}
    		for (int i = 31; i >= 0; i--) {
    			if (a >= x[i]) {
    				a -= x[i];
    				y[i] = true;
    				n++;
    			}
    		}
    		if (n > b) {
    			int d = n - b;
    			for (int i = 0; i < y.length; i++) {
    				if (y[i] && d == 0) {
    					f = x[i];
    					break;
    				}
    				if (y[i] && d != 0) {
    					m += x[i];
    					d--;
    				}
    			}
    			System.out.println(f - m);
    		} else {
    			System.out.println(0);
    		}
     
    	}
     
    	public static class FastScanner {
    		private BufferedReader br;
    		private StringTokenizer st;
     
    		public FastScanner() {
    			br = new BufferedReader(new InputStreamReader(System.in));
    		}
     
    		public String nextToken() {
    			while (st == null || !st.hasMoreElements()) {
    				try {
    					st = new StringTokenizer(br.readLine());
    				} catch (Exception e) {
     
    				}
     
    			}
    			return st.nextToken();
    		}
     
    		public int nextInt() {
    			return Integer.valueOf(nextToken());
    		}
    	}
     
    }
    
  • 相关阅读:
    linux 运维
    mariadb replication
    phpmyadmin
    Objective-C设计模式——单例Singleton(对象创建)
    收藏iOS学习资料
    axios拦截器
    vue单页面优化
    html设置http缓存代码
    js数组去重,排序的几种方法
    前端移动端问题
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947055.html
Copyright © 2020-2023  润新知