第十二届蓝桥杯省赛第一场B组

A:ASC(Java)

76

A:空间(C++)

67108864

B:卡片

3181

C:直线

40257

D:货物摆放

2430

E:路径

10266837

F:时间显示

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong();
		sc.close();
		Date date = new Date(x);
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		df.setTimeZone(TimeZone.getTimeZone("GMT"));
		System.out.println(df.format(date));
	}
}

G:最少砝码(Java)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		sc.close();
		long w = 1L;
		long i = 1L;
		long s = 1L;
		while (s < n) {
			w *= 3;
			s += w;
			i++;
		}
		System.out.println(i);
	}
}

G:砝码称重(C++)

import java.util.Scanner;

public class Main {

	static int N = 110;
	static int M = 200010;
	static int B = M / 2;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] w = new int[N];
		int m = 0;
		for (int i = 1; i <= n; i++) {
			w[i] = sc.nextInt();
			m += w[i];
		}
		sc.close();
		boolean[][] f = new boolean[N][M];
		f[0][B] = true;
		for (int i = 1; i <= n; i++) {
			for (int j = -m; j <= m; j++) {
				f[i][j + B] = f[i - 1][j + B];
				if (j - w[i] >= -m) {
					f[i][j + B] |= f[i - 1][j - w[i] + B];
				}
				if (j + w[i] <= m) {
					f[i][j + B] |= f[i - 1][j + w[i] + B];
				}
			}
		}
		int res = 0;
		for (int j = 1; j <= m; j++) {
			if (f[n][j + B]) {
				res++;
			}
		}
		System.out.println(res);
	}

}

H:杨辉三角形

import java.util.Scanner;

public class Main {

	static int n;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		sc.close();
		for (int k = 16;; k--) {
			if (check(k)) {
				break;
			}
		}
	}

	private static boolean check(int k) {
		long l = k * 2;
		long r = Math.max((long) n, l);
		while (l < r) {
			long mid = l + r >> 1;
			if (C((int) mid, k) >= n) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		if (C((int) r, k) != n) {
			return false;
		}
		System.out.println(r * (r + 1) / 2 + k + 1);
		return true;
	}

	private static long C(int a, int b) {
		long res = 1;
		for (int i = a, j = 1; j <= b; i--, j++) {
			res = res * i / j;
		}
		if (res > n) {
			return res;
		}
		return res;
	}
}

I:双向排序

待续

J:括号序列

import java.util.Scanner;

public class Main {

	static int N = 5010;
	static int MOD = (int) 1e9 + 7;
	static int n;
	static char[] str = new char[N];
	static long[][] f;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		sc.close();
		str = (" " + s).toCharArray();
		n = s.length();
		long l = work();
		str = (" " + reverse(s)).toCharArray();
		for (int i = 1; i <= n; i++) {
			if (str[i] == '(') {
				str[i] = ')';
			} else {
				str[i] = '(';
			}
		}
		long r = work();
		System.out.println(l * r % MOD);
	}

	private static long work() {
		f = new long[N][N];
		f[0][0] = 1;
		for (int i = 1; i <= n; i++) {
			if (str[i] == '(') {
				for (int j = 1; j <= n; j++) {
					f[i][j] = f[i - 1][j - 1];
				}
			} else {
				f[i][0] = (f[i - 1][0] + f[i - 1][1]) % MOD;
				for (int j = 1; j <= n; j++) {
					f[i][j] = (f[i - 1][j + 1] + f[i][j - 1]) % MOD;
				}
			}
		}
		for (int i = 0; i <= n; i++) {
			if (f[n][i] > 0) {
				return f[n][i];
			}
		}
		return -1;
	}

	private static String reverse(String s) {
		return new StringBuffer(s).reverse().toString();
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值