D
The main idea of algorithm is that the quantity is accepted before the value (as dozens before units)We're multiplying by 10,divided by the maximum error
not more than 1.for {10, 10, 2, 2}:2 * 10 + 10 / 10 for tens = 21;3 * 10 + 2 / 10 for double = 30.2In this case, the formula shall be used to bring the number to the range from 0 to 1: ((число-min) / (max-min))
code:using System;
using System.Collections.Generic;
using System.Linq;
const int countItems = 1000001;
List<int> a = new List<int>();
for (int i = 0; i < countItems; i++)
a.Add(0);
for (int i = 0; i < countItems + 1; i++)
a.Add(0);
int countWeight = a.Count() / 2 + 1;
int max = a.Max();
int min = a.Min();
if (max != min){ // избегаем деления на ноль
IGrouping<int, int> most =
a.GroupBy(n => n)
.OrderByDescending(
(n) => n.Count() * countWeight + ((n.Sum()-min) / (max-min))
)
.First();
Console.WriteLine("Наиболее часто встречается {0} в количестве {1}", most.Key, most.Count());
} else {
Console.WriteLine("Наиболее часто встречается {0} в количестве {1}", min, a.Count());
}
As a supplement to the topic, I tried to implement this algorithm in чистом виде js
When snipe is completed, three calculation are used.const find = (numA, numB, numC, countA, countB, countC, formulaCountWeight) => {
let weightA = 0;
let weightB = 0;
let weightC = 'отсутствует';
let a = [];
for (let i = 0; i < countA; i++)
a.push(numA);
for (let i = 0; i < countB; i++)
a.push(numB);
for (let i = 0; i < countC; i++)
a.push(numC);
const countWeight = [
1 / a.length / 2 + 1,
a.length / 2 + 1,
10
][formulaCountWeight];
// const max = Math.max.apply(null,a);
// const min = Math.min.apply(null, a);
// не справлялись на больших числах
const {max, min} = a.reduce((acc, e) => (
{
max: acc.max < e ? e: acc.max,
min: acc.min > e ? e: acc.min,
}
), {max: -Infinity, min: Infinity})
if (max != min){
let res = {}
// подсчет количества
res = a.reduce((acc, e) => (
{...acc, [e] : acc[e] ? {count: acc[e].count + 1} : {count: 1} }
), {})
// вычисление веса
res = Object.keys(res)
.map(n => (
{
weight : res[n].count * countWeight + ((+n-min) / (max-min)),
num: n,
count: res[n].count
}))
weightA = res[0].weight;
weightB = res[1].weight;
if (res[2]) weightC = res[2].weight;
// сотрировка по убыванию веса
res.sort((a, b) => b.weight - a.weight)
resultNum = res[0].num;
resultCount = res[0].count;
} else {
resultNum = min;
resultCount = countA + countB;
}
return { weightA, weightB, weightC, resultNum, resultCount }
}
const form = document.querySelector("form");
form.onsubmit = function(e){
e.preventDefault();
const { weightA, weightB, weightC, resultNum, resultCount } =
find(
+document.querySelector("[name=a]").value,
+document.querySelector("[name=b]").value,
+document.querySelector("[name=c]").value,
+document.querySelector("[name=cnt_a]").value,
+document.querySelector("[name=cnt_b]").value,
+document.querySelector("[name=cnt_c]").value,
+document.querySelector("[name=formula_countWeight]").value,
)
document.querySelector('#weight_a').textContent = weightA;
document.querySelector('#weight_b').textContent = weightB;
document.querySelector('#weight_c').textContent = weightC;
document.querySelector('#num').textContent = resultNum;
document.querySelector('#count').textContent = resultCount;
}<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
span { font-weight: bold; }
div { display: flex; margin: 5px; }
</style>
</head>
<body>
<form>
<div>
<label>число a:<input type="number" value="1" name="a"></label>
<label> в количестве:<input type="number" value="1000002" name="cnt_a"></label>
</div>
<div>
<label>число b:<input type="number" value="0" name="b"></label>
<label> в количестве:<input type="number" value="1000002" name="cnt_b"></label>
</div>
<div>
<label>число с:<input type="number" value="2" name="c"></label>
<label> в количестве: <input type="number" value="1000002" name="cnt_c"></label>
</div>
<div>
<label>
формула веса
<select name="formula_countWeight">
<option value="0" selected="selected" >= 1 / length / 2 + 1</option>
<option value="1" >= length / 2 + 1</option>
<option value="2" >= 10</option>
<select>
</label>
</div>
<button id="btn">Найти</button>
<form>
<p>веса: а=<span id="weight_a"></span> b=<span id="weight_b"></span> c=<span id="weight_c"></span></p>
<p>результат: число <span id="num"></span> в количестве: <span id="count"></span></p>
</body>
</html>