表结构如下:create table t_money(id int primary key,amount int not null);// id 是序列号,amount是金额对如下数据进行查询:+----+--------+
| id | amount |
+----+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 5 |
| 5 | 2 |
| 6 | 8 |
| 7 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
+----+--------+
从表中选取若干条数据,使得被选出来的几条数据的amount(金额)字段之和等于10,要求存储过程能返回被选取出来的数据的序列号。
算法举例:2+3+5=10 返回序列号:1,3,4
说明:表中会有多种组合使金额字段相加等于10,此存储过程要求只要能找到任意一种组合就马上成功返回,不需要找其他的组合了。例如上面序列号为 1,3,4 的组合是第一种,能返回它们就行,至于后面的组合不用管。另外,组合没有个数的限制,可以3个组合成10,如算法举例,也可以2个组合,甚至单个组合,即可能表中有一个数据它的金额就是10,当然也可以是N个数字的组合。
上面对题目的文字说明可能有些啰嗦,我只是想让大家尽可能的明白我的意思,有兴趣的朋友对题意还有疑问的可以联系我。谢谢。
QQ:390000258
| id | amount |
+----+--------+
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 5 |
| 5 | 2 |
| 6 | 8 |
| 7 | 1 |
| 8 | 2 |
| 9 | 3 |
| 10 | 3 |
+----+--------+
从表中选取若干条数据,使得被选出来的几条数据的amount(金额)字段之和等于10,要求存储过程能返回被选取出来的数据的序列号。
算法举例:2+3+5=10 返回序列号:1,3,4
说明:表中会有多种组合使金额字段相加等于10,此存储过程要求只要能找到任意一种组合就马上成功返回,不需要找其他的组合了。例如上面序列号为 1,3,4 的组合是第一种,能返回它们就行,至于后面的组合不用管。另外,组合没有个数的限制,可以3个组合成10,如算法举例,也可以2个组合,甚至单个组合,即可能表中有一个数据它的金额就是10,当然也可以是N个数字的组合。
上面对题目的文字说明可能有些啰嗦,我只是想让大家尽可能的明白我的意思,有兴趣的朋友对题意还有疑问的可以联系我。谢谢。
QQ:390000258
用oracle的数组实现,先把所有的amount都取出来放到数组中,
例如a(id)= amount,然后用循环任意组合求和,直到求出结果。
as
ran number;
begin
for cur in 1..100 loop
ran:=round(dbms_random.value(1,5));
for cur1 in (select * from t_money) loop
if ran>1 then
for cur2 in (select * from t_money where id>cur1.id) loop
if ran>2 then
for cur3 in (select * from t_money where id>cur2.id) loop
if ran>3 then
for cur4 in (select * from t_money where id>cur3.id) loop
if ran>4 then
for cur5 in (select * from t_money where id>cur4.id)loop
if cur1.amount+cur2.amount+cur3.amount+cur4.amount+cur5.amount=sum_num then
dbms_output.put_line(cur1.id||' '||cur2.id||' '||cur3.id||' '||cur4.id||' '||cur5.id);
return;
end if;
end loop;
else
if cur1.amount+cur2.amount+cur3.amount+cur4.amount=sum_num then
dbms_output.put_line(cur1.id||' '||cur2.id||' '||cur3.id||' '||cur4.id);
return;
end if;
end if;
end loop;
else
if cur1.amount+cur2.amount+cur3.amount=sum_num then
dbms_output.put_line(cur1.id||' '||cur2.id||' '||cur3.id);
return;
end if;
end if;
end loop;
else
if cur1.amount+cur2.amount=sum_num then
dbms_output.put_line(cur1.id||' '||cur2.id);
return;
end if;
end if;
end loop;
else
if cur1.amount=sum_num then
dbms_output.put_line(cur1.id);
return;
end if;
end if;
end loop;
end loop;
dbms_output.put_line('没找到相应的组合');
end pro_test5;
begin
pro_test5(20);
end;1 2 3 4 6
用了比较笨的方法
期待更好的写法
as
v_str varchar2(20):=',';
v_id number;
v_count number;
v_tmp number :=0;
v_money number :=0;
begin
select count(0) into v_count from t_money;
loop
select count(0) into v_count from t_money where not instr(v_str,id)>0 and amount <= 10-v_money;
if v_count = 0 then
exit;
-- or restart
end if;
select id,amount into v_id,v_tmp from
(select id,amount from t_money where not instr(v_str,id)>0 and amount <= 10-v_money order by dbms_random.value)
where rownum <2;
v_money := v_money + v_tmp;
v_str := v_str || v_id ||',';
exit when v_money =10;
end loop;
O_str := v_str;
-- Error
end;
我把你的代码copy到oracle10g中然后运行,并加入以下代码
dbms_output.put_line(O_str);
输出为空啊
instr(v_str,id)改成
instr(v_str,','||id||',');
还有v_count=0时的处理也应该改下改后代码如下
create or replace procedure P_test(sum_num in number)
as
v_str varchar2(200):=',';
v_id number;
v_count number;
v_tmp number :=0;
v_money number :=0;
v_length number:=0;
v_count1 number;
begin
select count(0) into v_count1 from t_money;
loop
select count(0) into v_count from t_money where not instr(v_str,','||id||',')>0 and amount <= sum_num-v_money;
if v_count = 0 then
if v_length=v_count1 then
dbms_output.put_line('error:the input-value is too large');
return;
end if;
v_str:=',';
v_money:=0;
-- or restart
end if; select id,amount into v_id,v_tmp from
(select id,amount from t_money where not instr(v_str,','||id||',')>0 and amount <= sum_num-v_money order by dbms_random.value)
where rownum <2;
v_money := v_money + v_tmp;
v_str := v_str || v_id ||',';
v_length:=v_length+1;
exit when v_money =sum_num; end loop;
dbms_output.put_line('id组合: '||substr(v_str,2));
-- Error
end p_test;调用:begin
p_test(15);--可以换成任意数字
end;