-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
125 lines (111 loc) · 3.55 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recherche de Sous-ensemble</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script>
function findSubset(numbers, target, partial = [], solutions = []) {
let sum = partial.reduce((a, b) => a + b, 0);
if (sum === target) {
let sortedPartial = partial.slice().sort((a, b) => a - b);
if (!solutions.some(solution => solution.length === sortedPartial.length && solution.every((value, index) => value === sortedPartial[index]))) {
solutions.push(sortedPartial);
}
}
if (sum >= target) {
return;
}
for (let i = 0; i < numbers.length; i++) {
let remaining = numbers.slice();
let n = remaining.splice(i, 1)[0];
findSubset(remaining, target, partial.concat([n]), solutions);
}
return solutions;
}
function onSubmit() {
let numbersString = document.getElementById('numbers').value;
numbersString = numbersString.replace(/,/g, '.');
let target = parseFloat(document.getElementById('target').value);
let numbers = numbersString.split('\n').map(Number);
let solutions = findSubset(numbers, target);
let resultElement = document.getElementById('result');
resultElement.innerHTML = '';
if (solutions.length > 0) {
solutions.forEach(solution => {
let resultTitle = document.createElement('p');
resultTitle.textContent = "Sous-ensemble trouvé :";
resultElement.appendChild(resultTitle);
let resultList = document.createElement('ul');
solution.forEach(function(number) {
let listItem = document.createElement('li');
listItem.textContent = number;
resultList.appendChild(listItem);
});
resultElement.appendChild(resultList);
});
} else {
resultElement.textContent = "Aucun sous-ensemble trouvé";
}
}
</script>
</head>
<body>
<main>
<h1>Recherche de sous-ensemble</h1>
<div class="grid pt-2">
<div>
<label for="numbers">Entrez les nombres (un par ligne) :</label><br>
<textarea class="form-control" id="numbers" rows="20" cols="30" placeholder="Exemple: 7,49 343 2401 168.07"></textarea>
</div>
<div>
<div>
<label for="target">Somme cible :</label><br>
<input class="form-control" type="text" id="target" placeholder="Exemple: 2744">
</div>
<div class="d-grid">
<button class="btn btn-primary btn-block mt-2" type="button" onclick="onSubmit()">Trouver le sous-ensemble</button>
</div>
</div>
<div>
<p>Résultat :</p>
<span id="result"></span>
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
main {
margin-left: auto;
margin-right: auto;
padding: 2rem;
width: 1000px;
}
h1 {
text-align: center;
}
textarea {
resize: none;
}
.flexbox {
display: flex;
justify-content: space-between;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 1fr;
gap: 1rem;
}
</style>
</html>