无聊,蛋帖,来做题,有兴趣进!

AcHerat 2012-03-08 04:49:30
加精
数学题 A * B = C 用SQL求解
A、B、C 由( 1、2、3、4、5、6、7、8、9 ) 9 个数字中组成,且每个数字只能取一次,9 个数字必须全部取到,不得多取、重取
比方说,1738 * 4 = 6952

个人求得:


138 42 5796 138425796
157 28 4396 157284396
159 48 7632 159487632
186 39 7254 186397254
198 27 5346 198275346
297 18 5346 297185346
483 12 5796 483125796
1738 4 6952 173846952
1963 4 7852 196347852


不知答案是否对,等楼下结果。
...全文
1082 53 打赏 收藏 转发到动态 举报
写回复
用AI写文章
53 条回复
切换为时间正序
请发表友善的回复…
发表回复
benbenLY 2014-01-13
  • 打赏
  • 举报
回复
留个脚印
524929657雯 2014-01-04
  • 打赏
  • 举报
回复
无尘阁 2013-12-28
  • 打赏
  • 举报
回复
大牛们,求收留
弘恩 2012-08-13
  • 打赏
  • 举报
回复
看一看我的代码! 这个问题,我有N种解法。
下面的代码是个人觉得最快的,
如果再改进,可以改用 CHARINDEX



with a1 as
(select '1' as a1
union all select '2'
union all select '3'
union all select '4'
union all select '5'
union all select '6'
union all select '7'
union all select '8'
union all select '9'),
a2 as ( select a.a1 + b.a1 as a2 from a1 a ,a1 b where a.a1 <> b.a1 ),
b3 as ( select a.a1 + b.a1 + c.a1 as b3 from a1 a ,a1 b ,a1 c where a.a1 <> b.a1 and a.a1 <> c.a1 and b.a1 <> c.a1 ),
b4 as ( select a.a1 + b.a1 + c.a1+ d.a1 as b4 from a1 a ,a1 b ,a1 c ,a1 d where a.a1 <> b.a1 and a.a1 <> c.a1 and a.a1 <> d.a1 and b.a1 <> c.a1 and b.a1 <> d.a1 and c.a1 <> d.a1 ),
c4 as ( select a.a1 + b.a1 + c.a1+ d.a1 as c4 from a1 a ,a1 b ,a1 c ,a1 d where a.a1 <> b.a1 and a.a1 <> c.a1 and a.a1 <> d.a1 and b.a1 <> c.a1 and b.a1 <> d.a1 and c.a1 <> d.a1 )


select a1.a1 a, b4.b4 b, c4.c4 c from a1 ,b4 ,c4 where cast ( a1.a1 as int ) * cast ( b4.b4 as int ) = cast ( c4.c4 as int )
and a1.a1 + b4.b4 + c4.c4 like '%1%'
and a1.a1 + b4.b4 + c4.c4 like '%2%'
and a1.a1 + b4.b4 + c4.c4 like '%3%'
and a1.a1 + b4.b4 + c4.c4 like '%4%'
and a1.a1 + b4.b4 + c4.c4 like '%5%'
and a1.a1 + b4.b4 + c4.c4 like '%6%'
and a1.a1 + b4.b4 + c4.c4 like '%7%'
and a1.a1 + b4.b4 + c4.c4 like '%8%'
and a1.a1 + b4.b4 + c4.c4 like '%9%'

union all
select a2.a2 ,b3.b3 ,c4.c4 from a2 ,b3 ,c4 where cast ( a2.a2 as int ) * cast ( b3.b3 as int ) = cast ( c4.c4 as int )
and a2.a2 + b3.b3 + c4.c4 like '%1%'
and a2.a2 + b3.b3 + c4.c4 like '%2%'
and a2.a2 + b3.b3 + c4.c4 like '%3%'
and a2.a2 + b3.b3 + c4.c4 like '%4%'
and a2.a2 + b3.b3 + c4.c4 like '%5%'
and a2.a2 + b3.b3 + c4.c4 like '%6%'
and a2.a2 + b3.b3 + c4.c4 like '%7%'
and a2.a2 + b3.b3 + c4.c4 like '%8%'
and a2.a2 + b3.b3 + c4.c4 like '%9%'


纳须弥 2012-03-11
  • 打赏
  • 举报
回复
现在才知道什么叫做人才。。。。各位都是大神啊
ssccbb1 2012-03-11
  • 打赏
  • 举报
回复
小三V5
淘淘大师 2012-03-11
  • 打赏
  • 举报
回复
路过。。。
傅琦鸿 2012-03-11
  • 打赏
  • 举报
回复
lmkzzs 2012-03-11
  • 打赏
  • 举报
回复
不会 是新手 有那个哥哥姐姐教教俺捏 QQ1040634262
天亮后说晚安 2012-03-10
  • 打赏
  • 举报
回复
omg。。。我只能路过
追风筝的孩子 2012-03-10
  • 打赏
  • 举报
回复
omg。。。我只能路过
zhangtieqiang 2012-03-10
  • 打赏
  • 举报
回复
楼上说的对呀
zhangtieqiang 2012-03-10
  • 打赏
  • 举报
回复
楼上说的对呀
daerter 2012-03-10
  • 打赏
  • 举报
回复
不会。
sunylf 2012-03-10
  • 打赏
  • 举报
回复
有點難度.
arnold 2012-03-09
  • 打赏
  • 举报
回复
学习一下知识支持一个。
叶子 2012-03-09
  • 打赏
  • 举报
回复

--看着挺笨重的,我本地运行250-300毫秒之间。
;with m as (
select number as id from master..spt_values where type = 'p' and number between 1 and 9),
m1 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2 ,ltrim(t3.id) as c3 ,ltrim(t4.id) as c4
from m t1 cross join m t2 cross join m t3 cross join m t4
where t1.id <> t2.id and t1.id <> t3.id and t1.id <> t4.id
and t2.id <> t3.id and t2.id <> t4.id and t3.id <> t4.id
),
m2 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2 ,ltrim(t3.id) as c3
from m t1 cross join m t2 cross join m t3
where t1.id <> t2.id and t1.id <> t3.id and t2.id <> t3.id
),
m3 as (select ltrim(t1.id) as c1 ,ltrim(t2.id) as c2
from m t1 cross join m t2 where t1.id <> t2.id
),
m4 as ( select (a.c1 + a.c2 + a.c3) as c1 ,( b.c1 + b.c2 ) as c2 ,
ltrim(( a.c1 + a.c2 + a.c3 ) + ( b.c1 + b.c2 )+ ltrim(( a.c1 + a.c2 + a.c3 ) * 1 * ( b.c1+ b.c2 ))) as c3
from m2 a ,m3 b
where ( a.c1 + a.c2 + a.c3 ) * 1 * ( b.c1 + b.c2 ) < 9876 union all
select ( a.id ) ,( b.c1 + b.c2 + b.c3 + b.c4 ) ,
ltrim(ltrim(a.id) + ( b.c1 + b.c2 + b.c3 + b.c4 )+ ltrim(a.id * 1 * ( b.c1 + b.c2 + b.c3 + b.c4 )))
from m a ,m1 b
where a.id * 1 * ( b.c1 + b.c2 + b.c3 + b.c4 ) < 9876
)
select c1 ,c2 ,c3 from m4
where len(c3) = 9
and charindex('1', c3) > 0
and charindex('2', c3) > 0
and charindex('3', c3) > 0
and charindex('4', c3) > 0
and charindex('5', c3) > 0
and charindex('6', c3) > 0
and charindex('7', c3) > 0
and charindex('8', c3) > 0
and charindex('9', c3) > 0
airsethh 2012-03-09
  • 打赏
  • 举报
回复
我们云南要干旱死了,没水洗脸了
litter_man 2012-03-09
  • 打赏
  • 举报
回复
路过,帮顶
AcHerat 2012-03-09
  • 打赏
  • 举报
回复

--try

),t1 as
(
select id from #tb a
where id between 1 and 99
and not exists (select 1 from t0 where len(ltrim(a.id))-len(replace(ltrim(a.id),ltrim(id),'')) >= 2)
)
加载更多回复(33)

11,849

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 非技术版
社区管理员
  • 非技术版社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧