MENU

第七届蓝桥杯 Java A—— 寒假作业

March 15, 2019 • Read: 3179 • 算法阅读设置

现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:

□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □

每个方块代表 1~13 中的某一个数字,但不能重复。
比如:
6 + 7 = 13
9 - 8 = 1
3 * 4 = 12
10 / 2 = 5

以及:
7 + 6 = 13
9 - 8 = 1
3 * 4 = 12
10 / 2 = 5

就算两种解法。(加法,乘法交换律后算不同的方案)

你一共找到了多少种方案?

  • public class Main {
  • static int[] book = new int[13];
  • static int[] arr = new int[13];
  • static long ans;
  • public static void main(String[] args) {
  • dfs(0);
  • System.out.println(ans);
  • }
  • static void dfs(int idx) {
  • if (idx == 12) {
  • if (arr[9] == arr[10] * arr[11])
  • ans++;
  • } else if (idx == 3) {
  • if (arr[0] + arr[1] != arr[2])
  • return;
  • } else if (idx == 6) {
  • if (arr[3] - arr[4] != arr[5])
  • return;
  • } else if (idx == 9) {
  • if (arr[6] * arr[7] != arr[8])
  • return;
  • }
  • for (int i = 1; i <= 13; i++) {
  • if (book[i - 1] == 0) {
  • book[i - 1] = 1;
  • arr[idx] = i;
  • dfs(idx + 1);
  • book[i - 1] = 0;
  • }
  • }
  • }
  • }
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

  • OωO
  • |´・ω・)ノ
  • ヾ(≧∇≦*)ゝ
  • (☆ω☆)
  • (╯‵□′)╯︵┴─┴
  •  ̄﹃ ̄
  • (/ω\)
  • ∠( ᐛ 」∠)_
  • (๑•̀ㅁ•́ฅ)
  • →_→
  • ୧(๑•̀⌄•́๑)૭
  • ٩(ˊᗜˋ*)و
  • (ノ°ο°)ノ
  • (´இ皿இ`)
  • ⌇●﹏●⌇
  • (ฅ´ω`ฅ)
  • (╯°A°)╯︵○○○
  • φ( ̄∇ ̄o)
  • ヾ(´・ ・`。)ノ"
  • ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
  • (ó﹏ò。)
  • Σ(っ °Д °;)っ
  • ( ,,´・ω・)ノ"(´っω・`。)
  • ╮(╯▽╰)╭
  • o(*////▽////*)q
  • >﹏<
  • ( ๑´•ω•) "(ㆆᴗㆆ)
  • (。•ˇ‸ˇ•。)
  • 泡泡
  • 阿鲁
  • 颜文字