日期:2014-05-17  浏览次数:20406 次

求算法
例如:N=1、2、3、4、5、6、7、8、9
X=14
14=5+9 或 14=1+4+9 或 14=2+3+4+5 或 14=1+2+3+8…………………………
M就是那些加起来等于14的数字,显示出这些可能的组合数字,求这个算法,asp或者sql,谢

------解决方案--------------------
排列组合,这个最好在前端程序中实现.

------解决方案--------------------
参考 http://blog.163.com/pursuedream@yeah/blog/static/12447234220099191234712/
------解决方案--------------------
SQL code

;with maco as
(
select number as c from master..spt_values where type='p' and number between 0 and 9
)

select 
    case when a.c=0 then '' else ltrim(a.c)+'+' end
    +ltrim(b.c)+'+'+ltrim(c.c)+'+'+ltrim(d.c)+'=14'
from maco a ,maco b,maco c ,maco d
where a.c<b.c and b.c<c.c and c.c<d.c
and a.c+b.c+c.c+d.c=14
/*
1+4+9=14
1+5+8=14
1+6+7=14
2+3+9=14
2+4+8=14
2+5+7=14
1+2+3+8=14
1+2+4+7=14
1+2+5+6=14
3+4+7=14
3+5+6=14
1+3+4+6=14
2+3+4+5=14
*/