鱼C论坛

 找回密码
 立即注册
查看: 2729|回复: 4

题目93:利用四位数字以及算术法则,找出目标数的最长序列

[复制链接]
发表于 2016-8-11 23:17:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 欧拉计划 于 2016-8-11 23:19 编辑
Arithmetic expressions

By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four arithmetic operations (+, −, *, /) and brackets/parentheses, it is possible to form different positive integer targets.

For example,

8 = (4 * (1 + 3)) / 2
14 = 4 * (3 + 1 / 2)
19 = 4 * (2 + 3) − 1
36 = 3 * 4 * (2 + 1)


Note that concatenations of the digits, like 12 + 34, are not allowed.

Using the set, {1, 2, 3, 4}, it is possible to obtain thirty-one different target numbers of which 36 is the maximum, and each of the numbers 1 to 28 can be obtained before encountering the first non-expressible number.

Find the set of four distinct digits, a < b < c < d, for which the longest set of consecutive positive integers, 1 to n, can be obtained, giving your answer as a string: abcd.


题目:

通过使用集合 {1, 2, 3, 4} 中每个数字一次(用且只用一次),以及四种算术运算 (+, &#8722;, *, /) 和括号,我们可以得到不同的目标正整数。

例如:

8 = (4 * (1 + 3)) / 2
14 = 4 * (3 + 1 / 2)
19 = 4 * (2 + 3) &#8722; 1
36 = 3 * 4 * (2 + 1)


但是将相连接是不允许的,如 12 + 34。

使用集合 {1, 2, 3, 4} 可以得到 31 个目标数,其中最大的是 36。而且 1 到 28 中的每个数字都可以被表示,但是 29 不能被表示。

找出四个不同1位数的集合,a < b < c < d,能够形成最长的 1 到 n 的连续正整数集合。以 abcd 的形式给出你的答案。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-18 12:40:34 | 显示全部楼层
看到国外达人写得很牛逼的迭代器算法:
答案1258
  1. import itertools

  2. def number_tuple(m):
  3.     for i in xrange(1, m-2):
  4.         for j in xrange(i+1, m-1):
  5.             for k in xrange(j+1, m):
  6.                 for l in xrange(k+1, m+1):
  7.                     yield (i,j,k,l)

  8. def operations(t):
  9.     if len(t) == 1:
  10.         yield t[0]
  11.         return

  12.     for i, e in enumerate(t):
  13.         for result in operations(t[:i] + t[i+1:]):
  14.             yield e * result
  15.             yield e + result
  16.             yield e - result
  17.             yield result - e
  18.             if result != 0:
  19.                 yield e / float(result)

  20. def consecutive_digits(digits):
  21.     for i in itertools.count(1):
  22.         if i not in digits:
  23.             return i - 1

  24. max_digits = (0, None)
  25. for t in number_tuple(9):
  26.     results = set(int(n) for n in operations(t) if int(n) == n and n > 0)
  27.     max_digits = max(max_digits, (consecutive_digits(results), t))
  28. print ''.join(map(str, max_digits[1]))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-4 15:08:56 | 显示全部楼层
  1. from itertools import *

  2. op = [lambda x, y: x + y, lambda x, y: x - y,
  3.       lambda x, y: x * y, lambda x, y: x / y if y != 0 else 0]

  4. def count(n):
  5.   s = set()
  6.   for d1, d2, d3, d4 in permutations(n):
  7.     for o1, o2, o3 in product(op, repeat = 3):
  8.       for r in [o3(o2(o1(d1, d2), d3), d4), o2(o1(d1, d2), o3(d3, d4))]:
  9.         if r == int(r):
  10.           s.add(int(r))
  11.   return next(filter(lambda x: x not in s, range(1, 999999)))

  12. ans = max(combinations(range(10), 4), key = count)
  13. print(''.join(map(str, ans)))
复制代码


https://github.com/devinizz/project_euler/blob/master/page02/93.py
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-26 18:20:03 | 显示全部楼层
本帖最后由 guosl 于 2022-1-26 18:21 编辑
  1. /*
  2. 答案:1258
  3.       可以表达从1到51之间的所有数
  4. 耗时:0.0586518秒
  5. */
  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace std;

  10. int gcd(int x, int y)//求最大公因数,用于分数的既约
  11. {
  12.   if (y == 0)
  13.     return x;
  14.   int z = x % y;
  15.   while (z != 0)
  16.   {
  17.     x = y;
  18.     y = z;
  19.     z = x % y;
  20.   }
  21.   return y;
  22. }

  23. struct stF//分数
  24. {
  25.   int n, d;
  26.   stF(void)
  27.   {
  28.     n = 0;
  29.     d = 1;
  30.   }
  31.   stF(int x, int y = 1)
  32.   {
  33.     int z = gcd(x, y);
  34.     n = x / z;
  35.     d = y / z;
  36.     if (d < 0)
  37.     {
  38.       n = -n;
  39.       d = -d;
  40.     }
  41.   }
  42.   stF(const stF &f)
  43.   {
  44.     n = f.n;
  45.     d = f.d;
  46.   }
  47.   const stF& operator=(const stF &f)
  48.   {
  49.     n = f.n;
  50.     d = f.d;
  51.     return *this;
  52.   }
  53.   bool operator<(const stF &f) const
  54.   {
  55.     return (n*f.d < d*f.n);
  56.   }
  57.   bool operator==(const stF &f) const
  58.   {
  59.     return (n*f.d == d * f.n);
  60.   }
  61.   stF operator+(const stF &f) const
  62.   {
  63.     int x = n * f.d + d * f.n;
  64.     int y = d * f.d;
  65.     stF g(x, y);
  66.     return g;
  67.   }
  68.   stF operator-(const stF &f) const
  69.   {
  70.     int x = n * f.d - d * f.n;
  71.     int y = d * f.d;
  72.     stF g(x, y);
  73.     return g;
  74.   }
  75.   stF operator*(const stF &f) const
  76.   {
  77.     int x = n * f.n;
  78.     int y = d * f.d;
  79.     stF g(x, y);
  80.     return g;
  81.   }
  82.   stF operator/(const stF &f) const
  83.   {
  84.     int x = n * f.d;
  85.     int y = d * f.n;
  86.     stF g(x, y);
  87.     return g;
  88.   }
  89. };

  90. int main(void)
  91. {
  92.   int nMax = 0;
  93.   char str[8];
  94.   str[4] = 0;
  95.   for (int i0 = 1; i0 <= 6; ++i0)
  96.   {
  97.     for (int i1 = i0 + 1; i1 <= 7; ++i1)
  98.     {
  99.       for (int i2 = i1 + 1; i2 <= 8; ++i2)
  100.       {
  101.         for (int i3 = i2 + 1; i3 <= 9; ++i3)
  102.         {
  103.           int a[4];
  104.           a[0] = i0;
  105.           a[1] = i1;
  106.           a[2] = i2;
  107.           a[3] = i3;
  108.           vector<int> vr;
  109.           do
  110.           {
  111.             vector<stF> v1, v2, v3;
  112.             stF f0(a[0]), f1(a[1]), f2(a[2]), f3(a[3]);
  113.             //f0与f1以及f2与f3的各种计算
  114.             v1.push_back(f0 + f1);
  115.             v2.push_back(f2 + f3);
  116.             v1.push_back(f0 * f1);
  117.             v2.push_back(f2 * f3);
  118.             v1.push_back(f0 - f1);
  119.             v2.push_back(f2 - f3);
  120.             v1.push_back(f1 - f0);
  121.             v2.push_back(f3 - f2);
  122.             v1.push_back(f0 / f1);
  123.             v2.push_back(f2 / f3);
  124.             v1.push_back(f1 / f0);
  125.             v2.push_back(f3 / f2);
  126.             //(xxx) xx (xxx)的各种计算
  127.             for (auto itr1 = v1.begin(); itr1 != v1.end(); ++itr1)
  128.             {
  129.               for (auto itr2 = v2.begin(); itr2 != v2.end(); ++itr2)
  130.               {
  131.                 stF g1 = *itr1, g2 = *itr2;
  132.                 v3.push_back(g1 + g2);
  133.                 v3.push_back(g1 * g2);
  134.                 v3.push_back(g1 - g2);
  135.                 v3.push_back(g1 / g2);
  136.                 v3.push_back(g2 - g1);
  137.                 v3.push_back(g2 / g1);
  138.               }
  139.             }
  140.             //将结果放入vr
  141.             for (auto itr = v3.begin(); itr != v3.end(); ++itr)
  142.             {
  143.               if (itr->n >= 1 && itr->d == 1)
  144.                 vr.push_back(itr->n);
  145.             }
  146.             //(((xxx?xxx)?xxx)?xxx)的各种计算
  147.             v2.clear();
  148.             for (auto itr = v1.begin(); itr != v1.end(); ++itr)
  149.             {
  150.               stF g1 = *itr, g2 = stF(a[2]);
  151.               v2.push_back(g1 + g2);
  152.               v2.push_back(g1 * g2);
  153.               v2.push_back(g1 - g2);
  154.               v2.push_back(g1 / g2);
  155.               v2.push_back(g2 - g1);
  156.               v2.push_back(g2 / g1);
  157.             }
  158.             v3.clear();
  159.             for (auto itr = v2.begin(); itr != v2.end(); ++itr)
  160.             {
  161.               stF g1 = *itr, g2 = stF(a[3]);
  162.               v3.push_back(g1 + g2);
  163.               v3.push_back(g1 * g2);
  164.               v3.push_back(g1 - g2);
  165.               v3.push_back(g1 / g2);
  166.               v3.push_back(g2 - g1);
  167.               v3.push_back(g2 / g1);
  168.             }
  169.             //将结果放入vr
  170.             for (auto itr = v3.begin(); itr != v3.end(); ++itr)
  171.             {
  172.               if (itr->n >= 1 && itr->d == 1)
  173.                 vr.push_back(itr->n);
  174.             }
  175.           } while (next_permutation(a, a + 4));//取四个数的下一个排列
  176.           //对结果排序去重
  177.           sort(vr.begin(), vr.end());
  178.           auto itr_end = unique(vr.begin(), vr.end());
  179.           //找出连续数
  180.           int k = 1;
  181.           for (auto itr = vr.begin(); itr != itr_end; ++itr, ++k)
  182.           {
  183.             if (*itr != k)
  184.               break;
  185.           }
  186.           //记录当前结果
  187.           if (k - 1 > nMax)
  188.           {
  189.             nMax = k - 1;
  190.             str[0] = '0' + i0;
  191.             str[1] = '0' + i1;
  192.             str[2] = '0' + i2;
  193.             str[3] = '0' + i3;
  194.           }
  195.         }
  196.       }
  197.     }
  198.   }
  199.   cout << str << endl << nMax << endl;
  200.   return 0;
  201. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-6-25 19:50:17 | 显示全部楼层
修改如下:
通过使用集合 {1, 2, 3, 4} 中每个数字一次(用且只用一次),以及四种算术运算 (+, -, *, /) 和括号,我们可以得到不同的目标正整数。

例如:

8 = (4 * (1 + 3)) / 2
14 = 4 * (3 + 1 / 2)
19 = 4 * (2 + 3) - 1
36 = 3 * 4 * (2 + 1)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 16:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表