求一个算法

沉默的疯子_ 2009-10-28 01:04:10
数据库记录里有 3条记录

记录1 20
记录2 20
记录3 20

输入一个数值 得到的效果如下

数值 50


记录1 0
记录2 0
记录3 10


给点思路 搞不定了 -.-!!
...全文
112 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaozhiyong110 2009-10-29
  • 打赏
  • 举报
回复
delphi 我不懂 没办法了
沉默的疯子_ 2009-10-28
  • 打赏
  • 举报
回复
这个数值是通过其他数据计算出来的 不用管它

现在比如数值结果是 n:=50;


*数据库记录
-------------
记录1 20
记录2 20
记录3 20
-------------
那么我开是循环并Edit数据库记录 减去记录的数值 一直减到n为0

比如 记录1的数据室20 那么n:=n-20 记录1=0;
n<>0的话下一条 这个循环不会做了
沉默的疯子_ 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yaozhiyong110 的回复:]
你用什么语言?
[/Quote]

delphi 数据库是 Access
沉默的疯子_ 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dinoalex 的回复:]
写得一般般,将就用吧

SQL code/*
create table t1 (a varchar(10),b int,c int)
insert into t1 values ('记录1',20,0)
insert into t1 values ('记录2',20,0)
insert into t1 values ('记录3',20,0)

drop table t1*//*
CREATE FUNCTION fs (@id int)
RETURNS int AS
BEGIN
DECLARE @s int
select @s= isnull(sum(b),0) from t2 where id < @id
select @s = @s+ b from t2 where id = @id
RETURN @s
END*//*

CREATE procedure dbo.updatetable
@v int
as
select IDENTITY(INT,1,1) as id,* into t2 from t1
update t2 set c = dbo.fs(id)
update t2 set c =0 where c <= @v
update t2 set c = c - @v where b = (select min(b) from t2 where b>0)
update t2 set c =0 where c <= 0
update t2 set b = c where c <= b
select * from t2
drop table t2

--drop procedure updatetable*/exec updatetable30--result1 记录1002 记录210103 记录32030
[/Quote]

看不懂 55555555~~~~~~~~~~~~~~~
yaozhiyong110 2009-10-28
  • 打赏
  • 举报
回复
意思是把结果减50?

那简单啊 得到表的结果集,然后用输入值(x)比较提取出来的值(y),如果(x)大的话,那么y = 0;x -= y;一直到 (x)小于 (y);那么y -= x;结束循环。
bdmh 2009-10-28
  • 打赏
  • 举报
回复
你结果的依据是怎样的,不能给个结果让大家凑数吧
dinoalex 2009-10-28
  • 打赏
  • 举报
回复
写得一般般,将就用吧

[Code=SQL]
/*
create table t1 (a varchar(10),b int,c int)
insert into t1 values ('记录1',20,0)
insert into t1 values ('记录2',20,0)
insert into t1 values ('记录3',20,0)

drop table t1
*/

/*
CREATE FUNCTION fs (@id int)
RETURNS int AS
BEGIN
DECLARE @s int
select @s= isnull(sum(b),0) from t2 where id < @id
select @s = @s+ b from t2 where id = @id
RETURN @s
END
*/

/*

CREATE procedure dbo.updatetable
@v int
as
select IDENTITY(INT,1,1) as id,* into t2 from t1
update t2 set c = dbo.fs(id)
update t2 set c =0 where c <= @v
update t2 set c = c - @v where b = (select min(b) from t2 where b>0)
update t2 set c =0 where c <= 0
update t2 set b = c where c <= b
select * from t2
drop table t2

--drop procedure updatetable
*/

exec updatetable 30

--result
1 记录1 0 0
2 记录2 10 10
3 记录3 20 30

[/Code]
沉默的疯子_ 2009-10-28
  • 打赏
  • 举报
回复
能具体点么
dinoalex 2009-10-28
  • 打赏
  • 举报
回复
数据库的话,用游标吧
yaozhiyong110 2009-10-28
  • 打赏
  • 举报
回复
你用什么语言?

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧