/**//* 描述:获取一组数据中3个和的绝对值最小的数据 在测试中,输入了有重复值的数据,有重复时,只当作一个数字 原题:N个int数,求其中三个数,使得其和的绝对值最小,如{-3,9,-5,90}的三个数是{-3,9,-5},要求分析时间和空间复杂度。 */ set nocount on ifexists(select1from sys.tables where name ='t') droptable t createtable t (id int,hi intdefault0 ) insertinto t(id) select-10 insertinto t(id) select-11 insertinto t(id) select-9 insertinto t(id) select6 insertinto t(id) select10 insertinto t(id) select-10 insertinto t(id) select6 insertinto t(id) select-6 insertinto t(id) select-10 insertinto t(id) select0 insertinto t(id) select-2 insertinto t(id) select15 insertinto t(id) select25 insertinto t(id) select-20 insertinto t(id) select-5 --输出测试数据 select*from t orderby id declare@idint,@op_idint,@min_idint declare cur cursorforselect id from t where id>0 declare@temptable(id1 int ,id2 int,id3 int ,hi int) open cur fetchnextfrom cur into@id while (@@fetch_status=0) begin selecttop1@op_id=min(abs(@id+id)) from t selecttop1@min_id=min(abs(@op_id+id)) from t insertinto@tempselect@id, @op_id-@id, @min_id-@op_id,@min_id update t set hi=@min_id where id=@id fetchnextfrom cur into@id end --输出需要测试的数据 select*from@tempwhere hi=(selectmin(hi) from@temp) close cur deallocate cur