gpt4 book ai didi

algorithm - 生成所有可能的客户和帐户组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:03:52 25 4
gpt4 key购买 nike

我正在开发一个应用程序来存储客户和帐户的关系数据(w.r.t 银行域)。通常在银行中,客户可以拥有一个单独的账户,也可以与其他客户拥有一个联名账户。

例1:客户C1有一个单独的账户A1。例 2:客户 C1 和 C2 有一个联名账户 JA1,其中 C1 是主要持有人,C2 是非主要持有人。

我正在寻找一种算法,该算法将为给定数量的客户和帐户生成所有可能的关系组合。

例如:如果客户数 = 2,帐户数 = 2,则算法应生成以下条目。

组合 #1:C1-A1-小学C1-A2-小学C2-A1-非主要C2-A2-非主要

组合 #2:C1-A1-小学C1-A2-非主要C2-A1-非主要C2-A2-初级

组合 #3:C1-A1-非主要C1-A2-小学C2-A1-初级C2-A2-非主要

组合 #4:C1-A1-非主要C1-A2-非主要C2-A1-初级C2-A2-初级

组合 #5:C1-A1-鞋底C1-A2-初级C2-A2-非主要

组合 #6:C1-A1-鞋底C1-A2-非主要C2-A2-初级

组合 #7:C1-A2-鞋底C1-A1-小学C2-A1-非主要

组合 #8:C1-A2-鞋底C1-A1-非主要C2-A1-初级

编辑:这不是完整的组合列表 - 但算法应该生成所有这些组合。

最佳答案

这里有 2 个问题需要解决:

  1. 获取 N 个客户所有可能的帐户类型。你可以这样做:

    const allAccounts = [];对于(让 i = 1;i <= customersNumber;i++){allAccounts.push(C${i}-Sole);对于(设 j = 1;j <= customersNumber;j++){如果 (i === j) 继续;allAccounts.push(C${i}-Primary C${j}-NonPrimary);}

对于 2 个客户,结果将是:

[
"C1-Sole",
"C1-Primary C2-NonPrimary",
"C2-Sole",
"C2-Primary C1-NonPrimary"
]
  1. 从此数组中获取长度为 r 的所有可能组合(带重复)。我们想在这里排除两种类型的组合:
  • 为同一客户拥有 2 个或更多单独帐户的客户。
  • 没有联系的人(没有共同的客户,如果我没猜错的话)

// checks if two accounts are connected
function connected(customers1, customers2) {
return customers1.filter(cu => customers2.includes(cu)).length > 0;
}

// checks if acc1 and acc2 are the same Sole account
function sameSoleAccount(acc1, acc2) {
return acc1.type === 'Sole' && acc1 === acc2;
}

function printAccount(i, a) {
const c = a.customers;
return a.type === 'Sole' ? `${c[0]}-A${i}-Sole` : `${c[0]}-A${i}-Primary ${c[1]}-A${i}-NonPrimary`;
}

function combination(chosen, arr, index, r) {
if (index === r) {
const combination = chosen.map((c, i) => printAccount(i + 1, arr[c])).join(', ');
console.log(combination);
return;
}
for (let i = 0; i < arr.length; i++) {
if (chosen.length === 0 ||
chosen.some(ch => !sameSoleAccount(arr[ch], arr[i])
&& connected(arr[ch].customers, arr[i].customers))) {
const copy = chosen.slice();
copy[index] = i;
combination(copy, arr, index + 1, r);
}
}
}

function allPossibleCombinations(accountsNumber, customersNumber) {
const allAccounts = [];
for (let i = 1; i <= customersNumber; i++) {
allAccounts.push({customers: [`C${i}`], type: 'Sole'});
for (let j = 1; j <= customersNumber; j++) {
if (i === j) continue;
allAccounts.push({customers: [`C${i}`, `C${j}`], type: 'Joint'});
}
}
console.log(`All possible combinations for ${customersNumber} customers and ${accountsNumber} accounts: `);
combination([], allAccounts, 0, accountsNumber);
}

allPossibleCombinations(2, 2);

关于algorithm - 生成所有可能的客户和帐户组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com