根据消费定额生成菜单的算法(原创)
生活随笔
收集整理的這篇文章主要介紹了
根据消费定额生成菜单的算法(原创)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在去年一個實驗性項目中有這樣一個問題,飯店根據顧客消費意愿進行菜單自動生成,如:699元套餐、899元套餐等。菜品類型有涼菜、熱菜、蒸菜等等,具體需求不詳細描述。關鍵是一個根據消費金額自動生成菜單的算法。可惜當時的代碼找不到了。
今天在論壇中又遇到一個類似問題,正好可用以回顧。如下:
表結構:
?
Table?created
?
Executed?in?0.468?seconds
插入數據:
CodeSQL>?insert?into?t_money?values(1,2);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(2,2);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(3,3);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(4,5);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(5,2);
?
1?row?inserted
?
Executed?in?0.016?seconds
?
SQL>?insert?into?t_money?values(6,8);
?
1?row?inserted
?
Executed?in?0.016?seconds
?
SQL>?insert?into?t_money?values(7,1);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(8,2);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(9,3);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?insert?into?t_money?values(10,3);
?
1?row?inserted
?
Executed?in?0?seconds
?
SQL>?select?substr(id,1,3)?id,substr(amount,1,3)?amount?from?t_money;
?
ID?????AMOUNT
------?------
1??????2
2??????2
3??????3
4??????5
5??????2
6??????8
7??????1
8??????2
9??????3
10?????3
?
10?rows?selected
?
Executed?in?0.156?seconds
?
問題是:
從表中選取若干條數據,使得被選出來的幾條數據的amount(金額)字段之和等于10,要求存儲過程能返回被選取出來的數據的序列號。 舉例:2+3+5=10 返回序列號:1,3,4?
存儲過程代碼:
create?or?replace?procedure?P_test(O_str?out?varchar2)exp user1/pwd@server owner=user1 file=c:\file.dmp
imp user2/pwd@server fromuser=user1 touser=user2 file=c:\file.dmp
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;
結果:,7,9,10,3,
轉載于:https://www.cnblogs.com/fyonline/archive/2009/08/26/1554178.html
總結
以上是生活随笔為你收集整理的根据消费定额生成菜单的算法(原创)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中兴机顶盒服务器地址大全,中兴机顶盒网络
- 下一篇: 河北光电机顶盒安装当贝桌面