array(
A=3,
B=6,
C=9
)
#include <iostream>
struct Sector
{
char name;
int total, counter, remainder;
bool allowNearby;
};
enum { N = 3 };
Sector secs[] { { 'A', 3 }, { 'B', 6 }, { 'C', 30 } };
int main()
{
int nTotal = 0;
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
nTotal += sec.total;
}
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
sec.counter = sec.total;
sec.remainder = sec.total;
sec.allowNearby = (sec.total * 2 > nTotal);
}
Sector* repeatS = nullptr;
for (int iSec = 0; iSec < nTotal; ++iSec) {
Sector* bestS = nullptr;
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
if (sec.remainder != 0 && &sec != repeatS) {
if (!bestS) { // первый подходящий?
bestS = &sec;
} else { // лучше bestS?
if (sec.counter < bestS->counter
|| (sec.counter == bestS->counter && sec.total > bestS->total))
bestS = &sec;
}
}
}
if (!bestS) // так и не нашли, что брать — берём repeatS
bestS = repeatS;
// пересчитаем счётчик и остаток
bestS->counter += nTotal * 2;
--bestS->remainder;
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
sec.counter -= sec.total * 2;
}
repeatS = bestS->allowNearby ? nullptr : bestS;
std::cout << bestS->name;
}
std::cout << std::endl;
}