楼主: newkid

[每日一题] 2022 PUZZLEUP

[复制链接]
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
21#
发表于 2022-11-4 10:29 | 只看该作者
#3

SQL>
SQL> with t as (select to_number(column_value) n from xmltable('1 to 4'))
  2  select count(*)
  3   from ( select level lvl,replace(sys_connect_by_path(n,','),',') nNum
  4            from t
  5          connect by level <= (select sum(n) from t)
  6        ) a
  7   where regexp_count(nNum,1,1) <= 1
  8     and regexp_count(nNum,2,1) <= 2
  9     and regexp_count(nNum,3,1) <= 3
10     and regexp_count(nNum,4,1) <= 4
11  /

  COUNT(*)
----------
     38847

这个先排列完再过滤,纯粹玩一下,很慢! connect by 的写法好像很难将 regexp_count的判断放到 CONNECT BY 子句中,因为前面的sys_connect_by_path 函数在这个子句中不支持,要是支持就没问题

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
22#
发表于 2022-11-4 10:33 | 只看该作者
newkid 发表于 2022-11-2 21:32
#3 FOUR DIGITS How many positive integers are there where only the digits 1, 2, 3 and 4 are used and ...

牛蛙这几天炒股亏得好惨!

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
23#
发表于 2022-11-4 11:44 | 只看该作者
newkid 发表于 2022-11-4 02:37
暴力法,和你们差不多:with n as (select level n from dual connect by level

从1-9只有用公式才能算出来

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
24#
发表于 2022-11-4 11:50 | 只看该作者
newkid 发表于 2022-11-4 02:37
暴力法,和你们差不多:with n as (select level n from dual connect by level

改写错在哪里
with recursive f(n,f) as (  ------- 构造阶乘表
select 0,1
union all
select n+1,f*(n+1) from f where n<10
)
,t(n,s,f) as (
select 1,lv-1,1 from (values(1,2))s(lv)
union all
select t.n+1,t.s+f.n,t.f*f.f
  from t
       ,f
where t.n<4 and f.n<=t.n+1
)     
select sum(f.f/t.f) from t,f where t.n=4 and t.s>0 and t.s=f.n;

sum
----
4024

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
25#
发表于 2022-11-4 12:55 | 只看该作者
〇〇 发表于 2022-11-4 11:50
改写错在哪里with recursive f(n,f) as (  ------- 构造阶乘表select 0,1 union allselect n+1,f*(n+1) fro ...

对values语法不熟悉导致

with recursive f(n,f) as (  ------- 构造阶乘表
select 0,1
union all
select n+1,f*(n+1) from f where n<10
)
,t(n,s,f) as (
select 1,lv-1,1 from (values(1),(2))s(lv)
union all
select t.n+1,t.s+f.n,t.f*f.f
  from t
       ,f
where t.n<4 and f.n<=t.n+1
)     
select sum(f.f/t.f) from t,f where t.n=4 and t.s>0 and t.s=f.n;

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
26#
发表于 2022-11-4 13:05 | 只看该作者
还是算不出1-9,
D select factorial(45)a;
Error: Out of Range Error: Overflow in HUGEINT multiplication!
postgres=# select factorial(45)a;
                             a
-----------------------------------------------------------
119622220865480194561963161495657715064383733760000000000

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
27#
发表于 2022-11-4 13:18 | 只看该作者
〇〇 发表于 2022-11-4 13:05
还是算不出1-9,D select factorial(45)a;Error: Out of Range Error: Overflow in HUGEINT multiplication! ...

终于把1-9算出来了,公式都用了10秒

with recursive f(n,f) as (  ------- 构造阶乘表
select n, factorial(n) from generate_series(0,45) m(n)
)
,t(n,s,f) as (
select 1,lv-1,1*f.f from (values(1),(2))s(lv),f where f.n=1
union all
select t.n+1,t.s+f.n,t.f*f.f
  from t
       ,f
where t.n<9 and f.n<=t.n+1
)
select sum(f.f/t.f) from t,f where t.n=9 and t.s>0 and t.s=f.n;
                            sum
-----------------------------------------------------------
189295112473714316327855615439169023.00000000000000000000
(1 行记录)


时间:9179.060 ms (00:09.179)
如果把最后一行的=9改为=4
            sum
----------------------------
38847.00000000000000000000
(1 行记录)


时间:5503.563 ms (00:05.504)

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
28#
 楼主| 发表于 2022-11-4 21:59 | 只看该作者
徽章已转给两位。
这道题还有改善余地,比如说四个数的使用次数分别是1,2,3,2的情形和1,2,2,3的计算结果是一样的,可以合并,但是目前也没有什么好办法把它们列举出来。

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
29#
 楼主| 发表于 2022-11-9 22:54 | 只看该作者
#4 THREE CIRCLES

Two circles of the same size and different colors are placed randomly on a computer screen. These circles can be in one of 4 different states:



1. The circles are not touching.
2. The circles are tangent, i.e. touching at a single point.
3. The circles are overlapping, the first one is on top.
4. The circles are overlapping, the second one is on top.

How many different states are there for 3 circles with different colors?

Notes:
*All three circles should be seen on the screen and considered two-dimensional.
*The circles have a well defined order (top/bottom), e.g. if the first circle is on top of the second and the second circle is on top of the third, the third circle cannot be on top of the first.

电脑屏幕上随机放置两个大小相同、颜色不同的圆圈。这些圆圈可以处于 4 种不同状态之一:

1. 两圆没有接触。
2. 两圆相切,即在一个点相接。
3. 两圆交叠,第一个在上面。
4. 两圆交叠,第二个在上面。

3个不同颜色的圆圈有多少种不同的状态?

注意:
*所有三个圆圈都应该可以在屏幕上看到并被认为是二维的。
*圆圈有明确的顺序(上/下),例如, 假设第一个圆圈在第二个圆圈的上面,而第二个圆圈在第三个圆圈的上面,则第三个圆圈不能在第一个圆圈的下面。

------------
这道题2017出过:
http://www.itpub.net/thread-2090903-1-2.html

283楼。

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
30#
 楼主| 发表于 2022-11-17 01:35 | 只看该作者
#5 ONE AND TWO

You will generate a number using only the digits 1 and 2. Our condition is that no sequence of 3 or more digits consecutively repeats two times. How many digits is the largest number satisfying this condition?

Note: The sequence “xxxx” cannot be included in the number because “xxx” repeats twice.


你仅使用数字 1 和 2 生成一个数字。我们的条件是任何连续3个或更多数字的序列都不重复出现。满足这个条件的最大数是多少位?

注意:数字中不能包含“xxxx”序列,因为“xxx”重复了两次。

------------
原文表达不清,consecutively应该是说连续的数字,而不是连续出现。我已经在翻译时做了纠正。

对付这种油腻的题目,就得写一个油腻的SQL。

使用道具 举报

回复

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

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表