高手帮忙看看,我出啥问题了?!
微软面试题:
教授选出两个从2到9的数,把它们的和告诉学生甲,把它们的积告诉学生乙,让他们轮流猜这两个数
甲说:“我猜不出”
乙说:“我猜不出”
甲说:“我猜到了”
乙说:“我也猜到了”
问这两个数是多少
我写的代码如下:
-------------------------------
create table #tb(id int)
insert #tb values(2)
insert #tb values(3)
insert #tb values(4)
insert #tb values(5)
insert #tb values(6)
insert #tb values(7)
insert #tb values(8)
insert #tb values(9)
--求和组合
select cast(a.id as char(1))+ '+ '+cast(b.id as char(1))+ '= '+cast(a.id+b.id as char(2)) as
expression,a.id+b.id as result
,a.id as id1,b.id as id2
into #amount
from #tb a,#tb b
where a.id <> b.id and a.id <b.id
--求积组合
select cast(a.id as char(1))+ '* '+cast(b.id as char(1))+ '= '+cast(a.id*b.id as char(2)) as
expression,a.id*b.id as result
,a.id as id1,b.id as id2
into #multiply
from #tb a,#tb b
where a.id <> b.id and a.id <b.id
--甲说:我猜不出---------------------------------------------
--删除求和表中两数相加的和只出现过一次的记录
delete a
from #amount a
where not exists(select 1 from #amount where result=a.result and id1 <> a.id1 and id2 <> a.id2)
--删除求积表中和中不存在的数据(可不要)
--delete a
--from #multiply a
--where not exists(select 1 from #amount where id1=a.id1 and id2=a.id2)
--乙说:我猜不出---------------------------------------------
--删除求积表中两数相乘的积只出现过一次的记录
delete a
from #multiply a
where not exists(select 1 from #multiply where result=a.result and id1 <> a.id1 and id2 <> a.id2)
--删除求和表中和中不存在的数据(可不要)
--delete a
--from #amount a
--where not exists(select 1 from #multiply where id1=a.id1 and id2=a.id2)
--甲说:我猜到了---------------------------------------------
--删除求和表中两数相加的和出现过一次以上的记录
delete #amount
where result in
( select a.result
from #amount a,#multiply b
where a.id1=b.id1 and a.id2=b.id2
group by a.result having count(*)> 1)
--删除求积表中和中不存在的数据(可不要)
--delete a
--from #multiply a
--where not exists(select 1 from #amount where id1=a.id1 and id2=a.id2)
--乙说:我也猜到了---------------------------------------------
--删除求积表中两数相加的和出现过一次以上的记录
delete #multiply
where result in
( select b.result
from #amount a,#multiply b
where a.id1=b.id1 and a.id2=b.id2
group by b.result having count(*)> 1)
--删除求和表中和中不存在的数据(可不要)
--delete a
--from #amount a