蓝桥杯18年B组

public class Main_1 {//第几天

	/**
	 * 第几天 2000年1月1日,是那一年的第1天, 那么,2000年5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(31 + 29 + 31 + 30 + 4);
	}

}

public class Main_2 {// 方格计数

	/**
	 * 我们以某个小方格的一个顶点为圆心画一个半径为1000的圆。 你能计算出这个圆里有多少个完整的小方格吗?
	 * 
	 * 注意:需要提交的是一个整数,不要填写任何多余内容。
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 方法1
		int r = 0;
		for (int i = 1; i <= 1000; i++) {
			for (int j = 1; j <= 1000; j++) {
				if (i * i + j * j <= 1000 * 1000)
					r++;
			}
		}
		System.out.println(r * 4);
		// 方法2
		int n = 1000;
		int y = n;
		int ans = 0;
		for (int x = 1; x <= n; x++) {
			while (x * x + y * y > n * n && y > 0)
				y--;
			ans += y;
		}
		System.out.println(ans * 4);
	}

}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigInteger;

public class Main_3 {

	/**
	 * 复数幂 题目:设i为虚数单位。对于任意正整数n,(2+3i)^n 的实部和虚部都是整数。 求 (2+3i)^123456 等于多少?
	 * 即(2+3i)的123456次幂,这个数字很大,要求精确表示。
	 * 
	 * 答案写成 “实部±虚部i” 的形式,实部和虚部都是整数(不能用科学计数法表示),中间任何地方都不加空格,实部为正时前面不加正号。(2+3i)^2
	 * 写成: -5+12i, (2+3i)^5 的写成: 122-597i 注意:需要提交的是一个很庞大的复数,不要填写任何多余内容。
	 * 
	 * 
	 * 
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger two = BigInteger.valueOf(2);
		BigInteger three = BigInteger.valueOf(3);
		BigInteger a = BigInteger.valueOf(2);
		BigInteger b = BigInteger.valueOf(3);
		BigInteger aa = null;
		BigInteger bb = null;
		for (int i = 0; i < 123455; i++) {
			aa = a.multiply(two).subtract(b.multiply(three));
			bb = a.multiply(three).add(b.multiply(two));
			a = aa;
			b = bb;
		}
		try {
			System.setOut(new PrintStream(new File("E:\\新建文本文档.txt")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(aa + (bb.compareTo(BigInteger.ZERO) < 0 ? "-" : "+")
				+ bb + "i");

	}

}

public class Main_4 {

	/**
	 * 测试次数 题目描述 x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。
	 * 各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通。
	 * 
	 * x星球有很多高耸入云的高塔,刚好可以用来做耐摔测试。塔的每一层高度都是一样的,与地球上稍有不同的是,他们的第一层不是地面,而是相当于我们的2楼。
	 * 
	 * 如果手机从第7层扔下去没摔坏,但第8层摔坏了,则手机耐摔指数=7。 特别地,如果手机从第1层扔下去就坏了,则耐摔指数=0。
	 * 如果到了塔的最高层第n层扔没摔坏,则耐摔指数=n
	 * 
	 * 为了减少测试次数,从每个厂家抽样3部手机参加测试。
	 * 
	 * 某次测试的塔高为1000层,如果我们总是采用最佳策略,在最坏的运气下最多需要测试多少次才能确定手机的耐摔指数呢?
	 * 
	 * 请填写这个最多测试次数。
	 * 
	 * 注意:需要填写的是一个整数,不要填写任何多余内容。
	 * 
	 * @param args
	 */
	static final int n = 1000;
	static int[] f1 = new int[n + 1];
	static int[] f2 = new int[n + 1];
	static int[] f3 = new int[n + 1];

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 1; i <= n; i++) {
			f1[i] = i;
		}
		for (int i = 1; i <= n; i++) {
			int ans = Integer.MAX_VALUE;
			for (int j = 1; j <= i; j++) {
				int _max = 1 + Math.max(f2[i - j], f1[j - 1]);
				ans = Math.min(ans, _max);
			}
			f2[i] = ans;
		}
		for (int i = 1; i <= n; i++) {
			int ans = Integer.MAX_VALUE;
			for (int j = 1; j <= i; j++) {
				int _max = 1 + Math.max(f3[i - j], f2[j - 1]);
				ans = Math.min(ans, _max);
			}
			f3[i] = ans;
		}
		System.out.println(f3[n]);
	}

}

import java.util.Random;

public class Main_5 {

	/**
	 * @param args
	 */
	public static int quickSelect(int a[], int l, int r, int k) {
		Random rand = new Random();
		int p = rand.nextInt(r - l + 1) + l;
		int x = a[p];
		int tmp = a[p];
		a[p] = a[r];
		a[r] = tmp;
		int i = l, j = r;
		while (i < j) {
			while (i < j && a[i] < x)
				i++;
			if (i < j) {
				a[j] = a[i];
				j--;
			}
			while (i < j && a[j] > x)
				j--;
			if (i < j) {
				a[i] = a[j];
				i++;
			}
		}
		a[i] = x;
		p = i;
		if (i - l + 1 == k)// (1)说明到底了
			return a[i];
		if (i - l + 1 < k)
			return quickSelect(a, i + 1, r, k - i + l - 1); // 填空
		// qsort(a, i + 1, right);
		// (3)先试试k,
		// (4)再考虑:k要移动到等于(i - l + 1),试试k-(i - l + 1)
		else
			// i - l + 1 > k
			return quickSelect(a, l, i - 1, k);// (2)qsort(a, left, i -
												// 1);对上了,k不变
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = { 1, 4, 2, 8, 5, 7 };
		System.out.println(quickSelect(a, 0, 5, 4));
	}

}

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Main_6 {

	/**
	 * 题目描述 给定三个整数数组 A = [A1, A2, … AN], B = [B1, B2, … BN], C = [C1, C2, … CN],
	 * 请你统计有多少个三元组(i, j, k) 满足:
	 * 
	 * 1 <= i, j, k <= N Ai < Bj < Ck 【输入格式】 第一行包含一个整数N。 第二行包含N个整数A1, A2, … AN。
	 * 第三行包含N个整数B1, B2, … BN。 第四行包含N个整数C1, C2, … CN。
	 * 
	 * 对于30%的数据,1 <= N <= 100 对于60%的数据,1 <= N <= 1000 对于100%的数据,1 <= N <= 100000
	 * 0 <= Ai, Bi, Ci <= 100000
	 * 
	 * 【输出格式】 一个整数表示答案
	 * 
	 * 【输入样例】 3  1 1 1  2 2 2  3 3 3
	 * 
	 * 【输出样例】 27
	 * 
	 * 资源约定: 峰值内存消耗(含虚拟机) < 256M CPU消耗 < 1000ms
	 * 
	 * 请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
	 * 不要使用package语句。不要使用jdk1.7及以上版本的特性。 主类的名字必须是:Main,否则按无效代码处理。
	 * ———————————————— 版权声明:本文为CSDN博主「南 墙」的原创文章,遵循CC 4.0
	 * BY-SA版权协议,转载请附上原文出处链接及本声明。
	 * 原文链接:https://blog.csdn.net/a1439775520/article/details/97613304
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		long ans = 0;
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		int[] c = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for (int i = 0; i < n; i++) {
			b[i] = sc.nextInt();
		}
		for (int i = 0; i < n; i++) {
			c[i] = sc.nextInt();
		}
		Arrays.sort(a);
		Arrays.sort(b);
		Arrays.sort(c);
		int p = 0;
		int q = 0;
		for (int i = 0; i < n; i++) {
			while (p < n && a[p] < b[i])
				p++;
			while (q < n && c[q] <= b[i])
				q++;
			ans += 1L * p * (n - q);
		}
		System.out.println(ans);
	}

}

import java.util.Scanner;

public class Main_7 {

	/**
	 * 例如dis(0, 1)=3, dis(-2, -1)=9
	 * 
	 * 给出整点坐标(X, Y),你能计算出dis(X, Y)吗?
	 * 
	 * 【输入格式】 X和Y
	 * 
	 * 对于40%的数据,-1000 <= X, Y <= 1000 对于70%的数据,-100000 <= X, Y <= 100000
	 * 对于100%的数据, -1000000000 <= X, Y <= 1000000000
	 * 
	 * 【输出格式】 输出dis(X, Y)
	 * 
	 * 【输入样例】 0 1
	 * 
	 * 【输出样例】 3
	 * 
	 * 资源约定: 峰值内存消耗(含虚拟机)< 256M CPU消耗 < 1000ms
	 * 
	 * 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
	 * 
	 * 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。 不要使用package语句。不要使用jdk1.7及以上版本的特性。
	 * 主类的名字必须是:Main,否则按无效代码处理。例如dis(0, 1)=3, dis(-2, -1)=9
	 * 
	 * 给出整点坐标(X, Y),你能计算出dis(X, Y)吗?
	 * 
	 * 【输入格式】 X和Y
	 * 
	 * 对于40%的数据,-1000 <= X, Y <= 1000 对于70%的数据,-100000 <= X, Y <= 100000
	 * 对于100%的数据, -1000000000 <= X, Y <= 1000000000
	 * 
	 * 【输出格式】 输出dis(X, Y)
	 * 
	 * 【输入样例】 0 1
	 * 
	 * 【输出样例】 3
	 * 
	 * 资源约定: 峰值内存消耗(含虚拟机)< 256M CPU消耗 < 1000ms
	 * 
	 * 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
	 * 
	 * 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。 不要使用package语句。不要使用jdk1.7及以上版本的特性。
	 * 主类的名字必须是:Main,否则按无效代码处理。
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong(), y = sc.nextLong();
		long d = 0;
		long n = 0;
		if (y > 0 && Math.abs(x) <= y) {
			n = y;
			d = y - x + 2 * y;
		} else if (x > 0 && Math.abs(y) <= x) {
			n = x;
			d = y + x;
		} else if (y <= 0 && x >= y - 1 && x <= -y) {
			n = -y;
			d = -(-y - x);
		} else if (x < 0 && y >= x + 1 && y <= -x) {
			n = -x - 1;
			d = -(y - x - 1 - 2 * x - 1);
		}
		System.out.println(sum(1L, 2 * n, 1) * 2 - d);
	}

	private static long sum(long a0, long n, int d) {
		// TODO Auto-generated method stub
		return (2 * a0 + (n - 1) * d) * n / 2;
	}

}

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main_8 {
	static class R {
		int ts, td;
	}

	/**
	 * 日志统计 小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:
	 * 
	 * ts id
	 * 
	 * 表示在ts时刻编号id的帖子收到一个"赞"。
	 * 
	 * 现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。
	 * 
	 * 具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。
	 * 
	 * 给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。
	 * 
	 * 【输入格式】 第一行包含三个整数N、D和K。 以下N行每行一条日志,包含两个整数ts和id。
	 * 
	 * 对于50%的数据,1 <= K <= N <= 1000 对于100%的数据,1 <= K <= N <= 100000 0 <= ts <=
	 * 100000 0 <= id <= 100000
	 * 
	 * 【输出格式】 按从小到大的顺序输出热帖id。每个id一行。
	 * 
	 * 【输入样例】 7 10 2     0 1    0 10    10 10   10 1    9 1     100 3     100 3
	 * 
	 * 【输出样例】 1 3
	 * 
	 * 资源约定: 峰值内存消耗(含虚拟机) < 256M CPU消耗 < 1000ms
	 * 
	 * 请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
	 * 
	 * 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。 不要使用package语句。不要使用jdk1.7及以上版本的特性。
	 * 主类的名字必须是:Main,否则按无效代码处理。
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), d = sc.nextInt(), k = sc.nextInt();
		R[] rs = new R[n];
		for (int i = 0; i < n; i++) {
			R r = new R();
			r.ts = sc.nextInt();
			r.td = sc.nextInt();
			rs[i] = r;
		}
		// 匿名内部类定义排序器
		Arrays.sort(rs, new Comparator<R>() {
			public int compare(R r1, R r2) {
				return r1.ts - r2.ts;
			}
		});
		// 用于给id计数
		Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
		// 用于存储答案(各个id),因为要求答案输出有序,这里直接用
		SortedSet<Integer> answers = new TreeSet<Integer>();
		// 尺取法
		int j = 0;
		for (int i = 0; i < n; ++i) {
			while (j < n && rs[j].ts - rs[i].ts < d) {
				int td = rs[j].td;
				Integer exist = cnt.get(td);
				if (exist != null) {
					cnt.put(td, exist + 1);
				} else {
					cnt.put(td, 1);// id第一次出现
				}
				if (cnt.get(td) >= k)
					answers.add(td);
				j++;
			}
			// (马上i就要更新了)将上一个i对应的id的计数-1
			Integer cntOfI = cnt.get(rs[i].td);
			if (cntOfI != null)
				cnt.put(rs[i].td, cntOfI - 1);
		}
		// 输出各个id
		for (Integer i : answers) {
			System.err.println(i);
		}
	}

}

import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main_9 {

	/**
	 * 
	 * 
	 * 
	 * @param args
	 */
	static int[] dx = { -1, 1, 0, 0 };
	static int[] dy = { 0, 0, -1, 1 };
	// 数据闺蜜
	private static int n;
	// 地图数据
	private static char[][] g;
	// 标记每个格子是否被访问
	private static int[][] mark;
	// 结果:被完全淹没的岛屿的数量
	private static int ans;

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		sc.nextLine();// 读取换行符
		// 初始化地图数据和标记数组
		g = new char[n][n];
		mark = new int[n][n];
		// 读取地图数据
		for (int i = 0; i < n; i++) {
			g[i] = sc.nextLine().toCharArray();
		}
		// 双重循环检测地图上的各个格子。以未被访问的#为起点,做宽搜
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (g[i][j] == '#' && mark[i][j] == 0) {
					bfs(i, j);
				}
			}
		}
		System.out.println(ans);
	}

	private static void bfs(int x, int y) {
		// TODO Auto-generated method stub
		mark[x][y] = 1;
		int cntOfBlock = 0;// 记录陆地的数量
		int cntOfSwed = 0;// 记录将被淹没的陆地的数量
		Queue<Point> queue = new LinkedList<Point>();
		queue.add(new Point(x, y));
		while (!queue.isEmpty()) {
			Point first = queue.poll();// 弹出头部
			cntOfBlock++;
			boolean swed = false;
			for (int d = 0; d < 4; d++) {
				int nx = first.x + dx[d];
				int ny = first.y + dy[d];
				if (0 <= nx && nx < n && 0 <= ny && ny < n) {
					if (g[nx][ny] == '.')
						swed = true;// 周边有一个。这块陆地就会被淹没,避免重复计数
					if (g[nx][ny] == '#' && mark[nx][ny] == 0) {
						queue.add(new Point(nx, ny));
						mark[nx][ny] = 1;
					}
				}
			}
			if (swed)
				cntOfSwed++;

		}
		if (cntOfBlock == cntOfSwed)
			ans++;
	}

	private static class Point {
		int x, y;

		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

}

import java.util.Scanner;

public class Main_10 {

	/**
	 * 题目描述 我们知道包含N个元素的堆可以看成是一棵包含N个节点的完全二叉树。
	 * 每个节点有一个权值。对于小根堆来说,父节点的权值一定小于其子节点的权值。
	 * 
	 * 假设N个节点的权值分别是1~N,你能求出一共有多少种不同的小根堆吗?
	 * 
	 * 例如对于N=4有如下3种:
	 * 
	 * 1 / \ 2 3 / 4
	 * 
	 * 1 / \ 3 2 / 4
	 * 
	 * 1 / \ 2 4 / 3
	 * 
	 * 由于数量可能超过整型范围,你只需要输出结果除以1000000009的余数。
	 * 
	 * 
	 * 【输入格式】 一个整数N。 对于40%的数据,1 <= N <= 1000 对于70%的数据,1 <= N <= 10000
	 * 对于100%的数据,1 <= N <= 100000
	 * 
	 * 【输出格式】 一个整数表示答案。
	 * 
	 * 【输入样例】 4
	 * 
	 * 【输出样例】 3
	 * 
	 * 
	 * 资源约定: 峰值内存消耗(含虚拟机) < 256M CPU消耗 < 1000ms
	 * 
	 * 
	 * 请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
	 * 
	 * 所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。 不要使用package语句。不要使用jdk1.7及以上版本的特性。
	 * 主类的名字必须是:Main,否则按无效代码处理。
	 * 
	 * 
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		f = new long[n + 5];
		inv = new long[n + 5];
		s = new int[n + 5];
		dp = new long[n + 5];
		f[0] = 1;
		for (int i = 1; i < n + 5; i++) {
			f[i] = f[i - 1] * i % mod;
			inv[i] = mpow(f[i], mod - 2);
		}

		for (int i = n; i >= 1; i--)
			// 类似堆的找孩子
			s[i] = 1 + (2 * i <= n ? s[2 * i] : 0)
					+ (2 * i + 1 <= n ? s[2 * i + 1] : 0);// c[i]<=n所以不用取余

		for (int i = 1; i < n + 5; i++)
			dp[i] = 1;

		for (int i = n; i >= 1; i--)
			if (2 * i + 1 <= n)
				dp[i] = dp[2 * i] * dp[2 * i + 1] % mod
						* C(s[i] - 1, s[i * 2 + 1]) % mod;// C(s[i]-1,s[i*2+1])和C(s[i]-1,s[i*2])都一样,组合对称

		System.out.println(dp[1]);
	}

	static int n;
	static long mod = 1000000009;
	static long[] f;
	static long[] inv;
	static int[] s;// 记录该点的孩子节点+自身的总数
	static long[] dp;

	static long C(int n, int m) {
		return f[n] * inv[m] % mod * inv[n - m] % mod;
	}

	static long mpow(long a, long n) {// 快速幂
		if (n == 0 || a == 1)
			return 1;
		long ans = 1;
		while (n != 0) {
			if (n % 2 == 1)
				ans = a * ans % mod;
			a = a * a % mod;
			n >>= 1;
		}

		return ans;
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值