蓝桥杯——暴力枚举拿省一
1.
题目描述
有一个 n \times mn×m 方格的棋盘,求其方格包含多少正方形、长方形(不包含正方形)。
输入格式
一行,两个正整数 n,mn,m(n \leq 5000,m \leq 5000n≤5000,m≤5000)。
输出格式
一行,两个正整数,分别表示方格包含多少正方形、长方形(不包含正方形)。
输入输出样例
输入 #1复制
2 3
输出 #1复制
8 10
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextInt(); long m = scanner.nextInt(); long zh = 0; long sh = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == j) { zh += (n - i) * (m - j); }else sh += (n - i) * (m - j); } } System.out.println(zh+" "+sh); }}
2.
题目描述
猪猪 Hanke 特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke 吃鸡很特别,为什么特别呢?因为他有 1010 种配料(芥末、孜然等),每种配料可以放 11 到 33 克,任意烤鸡的美味程度为所有配料质量之和。
现在, Hanke 想要知道,如果给你一个美味程度 nn ,请输出这 1010 种配料的所有搭配方案。
输入格式
一个正整数 nn,表示美味程度。
输出格式
第一行,方案总数。
第二行至结束,1010 个数,表示每种配料所放的质量,按字典序排列。
如果没有符合要求的方法,就只要在第一行输出一个 00。
输入输出样例
输入 #1复制
11
输出 #1复制
101 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
import java.util.Scanner;public class Main { private static String[] arr = new String[10005]; private static int ans = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); dfs(10,0,"",n); System.out.println(ans); for (int i = 0; i < ans; i++) { for (int j = 0; j < arr[i].length(); j++) { if (j == arr[i].length() - 1) System.out.print(arr[i].charAt(j)); else System.out.print(arr[i].charAt(j) + " "); } System.out.println(); } } private static void dfs(int count,int sum,String s,int n) { if (count == 0) { if (sum == n) { arr[ans++] = s; } s = ""; return; } dfs(count-1,sum+1,s+"1",n); dfs(count-1,sum+2,s+"2",n); dfs(count-1,sum+3,s+"3",n); }}
3.
题目描述
将 1, 2,\ldots, 91,2,…,9 共 99 个数分成三组,分别组成三个三位数,且使这三个三位数的比例是 A:B:CA:B:C,试求出所有满足条件的三个三位数,若无解,输出 No!!!
。
//感谢黄小U饮品完善题意
输入格式
三个数,A,B,CA,B,C。
输出格式
若干行,每行 33 个数字。按照每行第一个数字升序排列。
输入输出样例
输入 #1复制
1 2 3
输出 #1复制
192 384 576219 438 657273 546 819327 654 981
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int f = 0; for (int i = 123; i = 999 || m3 >= 999) break; if (m1 > m2 || m1 > m3 || m2 > m3) break; if (isC(String.valueOf(m1),String.valueOf(m2),String.valueOf(m3))) { if (isSuccess(a,b,c).equals(isSuccess(m1,m2,m3))) { f = 1; System.out.println(a + " " + b + " "+ c); } } } if (f == 0) System.out.println("No!!!"); } public static String isSuccess(int a,int b,int c) { for (int i = a; i >= 2; i--) { if (a % i == 0 && b % i == 0 && c % i == 0) { a /= i; b /= i; c /= i; } } return String.valueOf(a)+String.valueOf(b)+String.valueOf(c); } public static boolean isC(String a,String b,String c) { String arr = a + b + c; for (char i = '1'; i <= '9'; i++) { if (!arr.contains(String.valueOf(i))); return false; } return true; }}
4.
题目描述
排列与组合是常用的数学方法,其中组合就是从nn个元素中抽出rr个元素(不分顺序且r \le n)r≤n),我们可以简单地将nn个元素理解为自然数1,2,…,n1,2,…,n,从中任取rr个数。
现要求你输出所有组合。
例如n=5,r=3n=5,r=3,所有组合为:
12 3 , 1 2 4 , 1 2 5 , 1 3 4 ,1 3 5 , 1 4 5 , 2 3 4 , 2 3 5 , 2 4 5 , 3 4 5123,124,125,134,135,145,234,235,245,345
输入格式
一行两个自然数n,r(1<n<21,0 \le r \le n)n,r(1<n<21,0≤r≤n)。
输出格式
所有的组合,每一个组合占一行且其中的元素按由小到大的顺序排列,每个元素占三个字符的位置,所有的组合也按字典顺序。
**注意哦!输出时,每个数字需要33个场宽,pascal可以这样:
write(ans:3);
输入输出样例
输入 #1复制
5 3
输出 #1复制
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
import java.util.Scanner;public class Main { private static int n; private static int r; private static int[] arr; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); r = scanner.nextInt(); arr = new int[n]; backTrink(n,0,1); } public static void backTrink(int n,int count,int index) { if (count == r) { for (int i = 0; i < r; i++) { System.out.printf("%3d",arr[i]); } System.out.println(); return; } for (int i = index; i <= n; i++) { arr[count] = i; backTrink(n,count+1,i+1); arr[count] = 0; } }}