本文共 1832 字,大约阅读时间需要 6 分钟。
第五届2014年蓝桥杯国赛
暴力枚举每一个年龄相加的结果,再用2014与i相减作比较即可,需要加一个break,否则也会输出表弟的年龄。 public class Main { public static void main(String[] args) { for (int i = 1000; i < 2014; i++) { int age = i / 1000 + i / 100 % 10 + i / 10 % 10 + i % 10; if (age == 2014 - i) { System.out.print(i); break; } } } } 第八届2017年蓝桥杯省赛
使用dfs全排列模板,生成所有可能的排列,并通过条件判断来计数符合条件的排列数量。public class Main { static final int N = 10; static int cnt; static int[] a = new int[N]; static boolean[] used = new boolean[N]; public static void main(String[] args) { dfs(1); System.out.println(cnt / 3 / 2); // 去除旋转3种 镜像2种 } private static void dfs(int u) { if (u > 9) { int b1 = a[1] + a[2] + a[3] + a[4]; int b2 = a[4] + a[5] + a[6] + a[7]; int b3 = a[7] + a[8] + a[9] + a[1]; if (b1 == b2 && b2 == b3) cnt++; return; } else { for (int i = 1; i <= 9; i++) { if (!used[i]) { a[u] = i; used[i] = true; dfs(u + 1); used[i] = false; } } } } } 第三届2012年蓝桥杯省赛
通过动态规划解决一个博弈论问题,生成前10000项的结果数组。#includeusing namespace std; int main() { static final int N = 10010; int a[N] = {0}; Scanner sc(new Scanner(System.in)); int n = sc.nextInt(); for (int i = 1; i < 10000; i++) { if (a[i] == 0) { a[i + 1] = 1; a[i + 3] = 1; a[i + 7] = 1; a[i + 8] = 1; } } for (int i = 1; i <= n; i++) { int x = sc.nextInt(); cout << a[x] << endl; } return 0; }
转载地址:http://qzhfk.baihongyu.com/