楼主: newkid

[每日一题] 2022 PUZZLEUP

[复制链接]
论坛徽章:
519
奥运会纪念徽章:垒球
日期: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
11#
 楼主| 发表于 2022-10-28 02:54 | 只看该作者
用我的方法很容易推导出公式:

var n number;
exec :n:=8;

select (power(:n*(:n-1)/2,2)-(2*power(:n,3)-3*power(:n,2)+:n)/6)*4 from dual;


(POWER(:N*(:N-1)/2,2)-(2*POWER(:N,3)-3*POWER(:N,2)+:N)/6)*4
-----------------------------------------------------------
                                                       2576

使用道具 举报

回复
论坛徽章:
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
12#
发表于 2022-10-28 11:53 | 只看该作者
#2

--3*3
SQL>
SQL> with t(n,r,c) as (select level,ceil(level/3),decode(mod(level,3),0,3,mod(level,3))
  2                      from dual
  3                     connect by level <= 3*3),
  4       L(lvl,nlist,pre_r,pre_c,last_r,last_c,nsum,c_cnt,r_cnt,turn_cnt) as (select 1,
  5                                               cast(n as varchar2(1000)),
  6                                               null,
  7                                               null,
  8                                               t.r,
  9                                               t.c,
10                                               power(2,t.n),
11                                               1,
12                                               1,
13                                               0
14                                          from t
15                                         union all
16                                        select L.lvl + 1,
17                                               L.nlist||','||t.n,
18                                               L.last_r,
19                                               L.last_c,
20                                               t.r,
21                                               t.c,
22                                               L.nsum + power(2,t.n),
23                                               case when abs(L.last_c - t.c) = 1 then L.c_cnt + 1 else L.c_cnt end,
24                                               case when abs(L.last_r - t.r) = 1 then L.r_cnt + 1 else L.r_cnt end,
25                                               case when L.pre_c <> t.c and L.pre_r <> t.r then L.turn_cnt + 1 else L.turn_cnt end
26                                          from L,t
27                                         where abs(L.last_r - t.r) + abs(L.last_c - t.c) = 1
28                                           and bitand(L.nsum,power(2,t.n)) = 0
29                                           and case when L.turn_cnt = 0 then 1
30                                                    when L.turn_cnt = 1 and (L.pre_c = t.c or L.pre_r = t.r) then 1
31                                                    else 0
32                                               end = 1
33                                           and L.turn_cnt <=1 )
34  select count(distinct nsum) from L where turn_cnt = 1 and c_cnt <> r_cnt
35  /

COUNT(DISTINCTNSUM)
-------------------
                 16


--8*8
SQL>
SQL> with t(n,r,c) as (select level,ceil(level/8),decode(mod(level,8),0,8,mod(level,8))
  2                      from dual
  3                     connect by level <= 8*8),
  4       L(lvl,nlist,pre_r,pre_c,last_r,last_c,nsum,c_cnt,r_cnt,turn_cnt) as (select 1,
  5                                               cast(n as varchar2(1000)),
  6                                               null,
  7                                               null,
  8                                               t.r,
  9                                               t.c,
10                                               power(2,t.n),
11                                               1,
12                                               1,
13                                               0
14                                          from t
15                                         union all
16                                        select L.lvl + 1,
17                                               L.nlist||','||t.n,
18                                               L.last_r,
19                                               L.last_c,
20                                               t.r,
21                                               t.c,
22                                               L.nsum + power(2,t.n),
23                                               case when abs(L.last_c - t.c) = 1 then L.c_cnt + 1 else L.c_cnt end,
24                                               case when abs(L.last_r - t.r) = 1 then L.r_cnt + 1 else L.r_cnt end,
25                                               case when L.pre_c <> t.c and L.pre_r <> t.r then L.turn_cnt + 1 else L.turn_cnt end
26                                          from L,t
27                                         where abs(L.last_r - t.r) + abs(L.last_c - t.c) = 1
28                                           and bitand(L.nsum,power(2,t.n)) = 0
29                                           and case when L.turn_cnt = 0 then 1
30                                                    when L.turn_cnt = 1 and (L.pre_c = t.c or L.pre_r = t.r) then 1
31                                                    else 0
32                                               end = 1
33                                           and L.turn_cnt <=1 )
34  select count(distinct nsum) from L where turn_cnt = 1 and c_cnt <> r_cnt
35  /

COUNT(DISTINCTNSUM)
-------------------
               2576

老办法!

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期: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
13#
 楼主| 发表于 2022-10-28 21:30 | 只看该作者
solomon_007 发表于 2022-10-28 11:53
#2 --3*3SQL> SQL> with t(n,r,c) as (select level,ceil(level/3),decode(mod(level,3),0,3,mod(level,3)) ...

欢迎回来玩!你现在还搞不搞ORACLE?
你这个写法是把每个L形都找出来了,在这道题里面没有必要这么具体。OO的坐标写法就很简单。

使用道具 举报

回复
论坛徽章:
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
14#
发表于 2022-10-29 09:08 | 只看该作者
newkid 发表于 2022-10-28 21:30
欢迎回来玩!你现在还搞不搞ORACLE?你这个写法是把每个L形都找出来了,在这道题里面没有必要这么具体。OO的 ...

大哥好! 还在搞! 平时偶尔也潜水上来看看。
OO的坐标的是有个最简洁的写法,但记不得了,又不想翻老帖,就用自己的笨办法。

使用道具 举报

回复
论坛徽章:
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
15#
发表于 2022-10-29 09:21 | 只看该作者
OO的坐标写法,找到了,好像是这个写法:

SQL> select level n, trunc((level-1)/3) r, mod(level-1,3) c from dual connect by level<=3*3;

         N          R          C
---------- ---------- ----------
         1          0          0
         2          0          1
         3          0          2
         4          1          0
         5          1          1
         6          1          2
         7          2          0
         8          2          1
         9          2          2

9 rows selected

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期: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
16#
 楼主| 发表于 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 no digit is used more than the number it represents?

If the problem was asked for the digits 1 and 2 the answer would be 8

1, 2, 12, 21, 22, 122, 212, 221


有多少个正整数只用到数字 1、2、3 和 4,而且每个数字的使用次数都不超过它所代表的数?

如果问的是数字 1 和 2,答案将是 8:
1, 2, 12, 21, 22, 122, 212, 221

---------
这道题就是牛蛙所不齿的中学排列组合题。

使用道具 举报

回复
论坛徽章:
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
17#
发表于 2022-11-3 09:51 | 只看该作者
本帖最后由 〇〇 于 2022-11-3 09:53 编辑

貌似newkid的贴被吃掉了,
第3题:
How many positive integers are there where only the digits 1, 2, 3 and 4 are used and no digit is used more than the number it

represents?

If the problem was asked for the digits 1 and 2 the answer would be 8

1, 2, 12, 21, 22, 122, 212, 221

如果只使用数字1、2、3和4,并且没有数字出现的次数比它所表示的数更多,那么有多少这样的正整数?



如果问题被问到数字1和2,答案将是8

with recursive t as (select n::varchar from generate_series(1,4) t(n)),
s as (select 1 lv, n from t
union all
select lv+1,t.n||s.n n from s,t where
regexp_count(s.n,t.n)<t.n::int
)
select
count (1)
from s
;

使用道具 举报

回复
论坛徽章:
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
18#
发表于 2022-11-3 13:25 | 只看该作者
#2

SQL>
SQL> with t as (select level n from dual connect by level<=4),
  2       s(lvl,N) as (select 1,cast(n as varchar2(100)) from t
  3                     union all
  4                    select lvl + 1,
  5                           s.N||t.n
  6                      from s,t
  7                     where regexp_count(S.N,t.n,1) < t.n
  8                     )
  9  select count(*) from s
10  /

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


太简单了。

使用道具 举报

回复
论坛徽章:
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
19#
发表于 2022-11-3 13:27 | 只看该作者
搞错了,这是第 #3 题。

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期: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
20#
 楼主| 发表于 2022-11-4 02:37 | 只看该作者
暴力法,和你们差不多:
with n as (select level n from dual connect by level<=4)
,t(num) as (
select cast(n as varchar2(20)) from n
union all
select t.num||n
  from t,n
where regexp_count(t.num,n)<n
)
select count(*) from t;

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

PL/SQL排列公式:
declare
  s number :=0;
  
  function f(n in number) return number ---- 阶乘
  as
  begin
    return case when n in (0,1) then 1 else n*f(n-1) end;
  end;
begin
  -------- i,j,k,l 分别表示1,2,3,4的使用次数
  for i in 0..1 loop
      for j in 0..2 loop
          for k in 0..3 loop
              for l in 0..4 loop
                  if i+j+k+l>0 then
                     s := s+f(i+j+k+l)/(f(i)*f(j)*f(k)*f(l));
                  end if;
              end loop;
          end loop;
      end loop;
  end loop;
  dbms_output.put_line(s);
end;
/

38847

PL/SQL procedure successfully completed.
  
  
用SQL实现上述算法:
with f(n,f) as (  ------- 构造阶乘表
select 0,1 from dual
union all
select n+1,f*(n+1) from f where n<(select sum(level) from dual connect by level<=4)
)
,t(n,s,f) as (
select 1,level-1,1 from dual connect by level<=2
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(F.F/T.F)
------------
       38847

现在转不了徽章,等系统恢复了转给你们。

使用道具 举报

回复

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

本版积分规则 发表回复

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