我正在寻找您的专家的指导,以找出一种将数字与数字子集的总和进行比较的方法
DECLARE
L_NUM_TO_COMPARE NUMBER := 0;
L_NUM_SUBSET NUMBER := 0;
BEGIN
FOR MAIN_REC IN (
SELECT 1 ID, 25150 ASSIGN_AMT FROM DUAL
UNION ALL
SELECT 2 ID, 19800 ASSIGN_AMT FROM DUAL
UNION ALL
SELECT 3 ID, 27511 ASSIGN_AMT FROM DUAL
) LOOP
L_NUM_TO_COMPARE := MAIN_REC.ASSIGN_AMT;
DBMS_OUTPUT.PUT_LINE( L_NUM_TO_COMPARE);
FOR C IN (
SELECT 1 ID, 7120 WORK_AMT FROM DUAL
UNION ALL SELECT 2 ID, 8150 WORK_AMT FROM DUAL
UNION ALL SELECT 3 ID, 8255 WORK_AMT FROM DUAL
UNION ALL SELECT 4 ID, 9051 WORK_AMT FROM DUAL
UNION ALL SELECT 5 ID, 1220 WORK_AMT FROM DUAL
UNION ALL SELECT 6 ID, 12515 WORK_AMT FROM DUAL
UNION ALL SELECT 7 ID, 13555 WORK_AMT FROM DUAL
UNION ALL SELECT 8 ID, 5221 WORK_AMT FROM DUAL
UNION ALL SELECT 9 ID, 812 WORK_AMT FROM DUAL
UNION ALL SELECT 10 ID, 6562 WORK_AMT FROM DUAL
ORDER BY 2 DESC
) LOOP
L_NUM_SUBSET := NVL(L_NUM_SUBSET,0) + C.WORK_AMT;
DBMS_OUTPUT.PUT_LINE( L_NUM_SUBSET);
/*
I NEED TO PUT SOME LOGIC HOW CAN I FIND NEAREST SUM OF SUBSET
*/
IF MAIN_REC.ASSIGN_AMT = L_NUM_SUBSET THEN
DBMS_OUTPUT.PUT_LINE( L_NUM_SUBSET);
END IF;
END LOOP;
END LOOP;
END;
我一直在寻找这个论坛,发现一个问题, 子组数字的总和
这几乎是我的相同要求,我所需要的可以指出一点,如何在我拥有的PL / SQL中做到这一点(Oracle DB 11g R2)
提出问题的方式是,我假设您并不是真正在解决子集和问题,而是一个比较简单的问题,您想将数字与非常有限的子集进行比较,即子集的排序WORK_AMT
不断提升,没有差距。
可以单独使用Oracle SQL来解决:
WITH
ASSIGN(ID, ASSIGN_AMT) AS (
SELECT 1, 25150 FROM DUAL
UNION ALL SELECT 2, 19800 FROM DUAL
UNION ALL SELECT 3, 27511 FROM DUAL
),
VALS (ID, WORK_AMT) AS (
SELECT 1 , 7120 FROM DUAL
UNION ALL SELECT 2 , 8150 FROM DUAL
UNION ALL SELECT 3 , 8255 FROM DUAL
UNION ALL SELECT 4 , 9051 FROM DUAL
UNION ALL SELECT 5 , 1220 FROM DUAL
UNION ALL SELECT 6 , 12515 FROM DUAL
UNION ALL SELECT 7 , 13555 FROM DUAL
UNION ALL SELECT 8 , 5221 FROM DUAL
UNION ALL SELECT 9 , 812 FROM DUAL
UNION ALL SELECT 10, 6562 FROM DUAL
),
SUMS (ID, WORK_AMT, SUBSET_SUM) AS (
SELECT VALS.*, SUM (WORK_AMT) OVER (ORDER BY ID)
FROM VALS
)
SELECT
ASSIGN.ID,
ASSIGN.ASSIGN_AMT,
MIN (SUBSET_SUM) KEEP (
DENSE_RANK FIRST
ORDER BY ABS (ASSIGN_AMT - SUBSET_SUM)
) AS CLOSEST_SUM
FROM
ASSIGN
CROSS JOIN
SUMS
GROUP BY
ASSIGN.ID, ASSIGN.ASSIGN_AMT
以上收益:
ID ASSIGN_AMT CLOSEST_SUM
---------------------------
1 25150 29085
2 19800 20935
3 27511 29085
请注意,此问题在时间和空间上具有指数级的复杂性。对于WORK
表中的少量值,只能合理地解决它!
WITH
ASSIGN (ID, ASSIGN_AMT) AS (
SELECT 1, 25150 FROM DUAL
UNION ALL SELECT 2, 19800 FROM DUAL
UNION ALL SELECT 3, 27511 FROM DUAL
),
WORK (ID, WORK_AMT) AS (
SELECT 1 , 7120 FROM DUAL
UNION ALL SELECT 2 , 8150 FROM DUAL
UNION ALL SELECT 3 , 8255 FROM DUAL
UNION ALL SELECT 4 , 9051 FROM DUAL
UNION ALL SELECT 5 , 1220 FROM DUAL
UNION ALL SELECT 6 , 12515 FROM DUAL
UNION ALL SELECT 7 , 13555 FROM DUAL
UNION ALL SELECT 8 , 5221 FROM DUAL
UNION ALL SELECT 9 , 812 FROM DUAL
UNION ALL SELECT 10, 6562 FROM DUAL
),
SUMS (SUBSET_SUM, MAX_ID) AS (
SELECT WORK_AMT, ID FROM WORK
UNION ALL
SELECT WORK_AMT + SUBSET_SUM, GREATEST (MAX_ID, WORK.ID)
FROM SUMS JOIN WORK
ON SUMS.MAX_ID < WORK.ID
)
SELECT
ASSIGN.ID,
ASSIGN.ASSIGN_AMT,
MIN (SUBSET_SUM) KEEP (
DENSE_RANK FIRST
ORDER BY ABS (ASSIGN_AMT - SUBSET_SUM)
) AS CLOSEST_SUM
FROM SUMS
CROSS JOIN ASSIGN
GROUP BY ASSIGN.ID, ASSIGN.ASSIGN_AMT
现在产生:
ID ASSIGN_AMT CLOSEST_SUM
---------------------------
1 25150 25133
2 19800 19768
3 27511 27488
以下代码的输出为假 我不明白。我想它会返回true。在我为SCJP做准备时,了解其背后的原因可能会有所帮助。有人能帮忙吗?
如何比较两个列表是否相等验证数据来自Excel工作表。我需要验证两个列表是否相同,并且列表中没有附加元素或缺少元素。我不需要对列表进行排序。打印输出CAGID Excel data=CAGID Web列表
问题内容: 将代码点与Java字符进行比较的“正确”方法是什么?例如: 我知道我可以做到: 但是这段代码看起来很脆弱。有没有比较正式的API方法来,或转换到一个比较? 问题答案: 有一点背景知识:Java在1995年出现时,该类型基于原始的“ Unicode 88 ”规范,该规范限于16位。一年后,当Unicode 2.0实施时,替代字符的概念超出了16位的限制。 Java内部以utf-16格式表
我有一个Java8/Maven/Spring Boot项目。我使用Javers来审核应用程序中的更改,然而,我创建了一个自定义注释,它放在类中的字段上方,如果更改,我希望对其进行审核。下面是一个示例类:
我有一个本地数据库,一个登录页面和一个servlet。我可以从数据库中获得我想要的值,例如用户名和密码。我还可以在html登录表单中获得用户输入。我对HTML使用Request.GetParameter。我想检查数据库中的用户名是否等于输入的用户名,以及数据库中的密码是否等于输入的密码,否则访问被拒绝。这是我的代码: