= 100 (Kleinprojekte)
Verbinden Sie die Zahlen von 1 bis 9 mit Rechenzeichen, so dass das Ergebnis 100 ist:
1_2_3_4_5_6_7_8_9 = 100 .
Jedes _ ist mit einem Rechenzeichen der Grundrechenarten (Addition, Subtraktion, Division, Multiplikation) zu ersetzen.
Jedoch gilt keine Punkt-vor-Strich-Rechnung. Das Ergebnis wird von links nach rechts geparst.
(1+2×3) = (3 × 3) = 9,
nicht: (1+6) = 7.
Geben Sie alle möglichen Lösungen aus. Wieviele mögliche Lösungen gibt es?
0 Kommentare
3 Lösung(en)
import itertools
from fractions import Fraction
for o in itertools.product(["+", "-", "*", "/"], repeat=8):
total = Fraction(1, 1)
l = ["1"]
for i, n in enumerate(range(2, 10)):
total = eval(f"total {o[i]} n")
l.extend((o[i], str(n)))
if total == 100:
print(" ".join(l) + " = 100")
Lösung von: Name nicht veröffentlicht
function eval2(exp) { return Function(`return ${exp}`)(); }
function numToSigns(num) {
return (
num.toString(4)
.padStart(8, '0')
.replace(/0/g, '+') .replace(/1/g, '-')
.replace(/2/g, '*') .replace(/3/g, '/')
.split('');
);
}
function check(str) {
let c = str.split('');
while (c.length > 1)
c.unshift(eval2(c.splice(0, 3).join('')));
return c[0] == 100;
}
// main
let solutions = [],
max = parseInt('33333333', 4),
tmp, signs,
i = 0, j;
for (i; i <= max; i++) {
signs = numToSigns(i);
tmp = '';
j = 1;
while (j < 9) {
tmp += j + signs.shift();
j++;
}
tmp += j;
if (check(tmp)) solutions.push(tmp);
}
// ausgabe
document.write('<p>');
for (i of solutions) document.write(`${i.split('').join(' ')}<br>`);
document.write(`</p><p><b>${solutions.length} solvoj.</b></p>`);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
/*
Sieht zwar im ersten Moment aufgrund der acht ineinander-
verschachtelten Schleifen etwas unprofessionell und 'wirklich' nach
keinem guten Programmierstil aus, benötigt aber (inklusive Bildschirmausgabe)
weniger als 30 ms, da gerade einmal 4^8 = 65.536 Durchläufe nötig sind.
Ein rekursiver Versuch mit nur einer Schleife war deutlich langsamer.
C++ 17 wird vorausgesetzt.
*/
#include <iostream>
#include <array>
#include <tuple>
using uint = unsigned int;
template<typename _Ty>
inline const std::tuple<bool, _Ty> get_result(const _Ty& lhs_, const _Ty& rhs_, const _Ty& op_) {
switch (op_) {
case 0: return { true, lhs_ + rhs_ };
case 1: return (lhs_ < rhs_) ? std::make_tuple(false, 0u) : std::make_tuple(true, (lhs_ - rhs_));
case 2: return { true, lhs_ * rhs_ };
default: return (lhs_ < rhs_) ? std::make_tuple(false, 0u) : std::make_tuple(true, (lhs_ / rhs_));
}
}
template<typename _Ty>
inline const auto compute(const _Ty& cont_) {
const std::array<char, 4>operators{ '+', '-', '*', '/' };
std::string str{};
uint res{ 1 };
for (uint i{ 2 }; i < 10; ++i) {
const auto& [gt0, tmp] = get_result(res, i, cont_[i - 2ULL]); // C++ 17!
if (!gt0) return;
res = tmp;
}
if (res == 100) {
for (uint i{ 1 }; i < 9; ++i) {
str.push_back(i + '0');
str.push_back(operators[cont_[i - 1]]);
}
std::cout << str << 9 << "\n";
}
}
inline const auto iter() {
for (uint _1{ 0 }; _1 < 4; ++_1)
{
for (uint _2{ 0 }; _2 < 4; ++_2)
{
for (uint _3{ 0 }; _3 < 4; ++_3)
{
for (uint _4{ 0 }; _4 < 4; ++_4)
{
for (uint _5{ 0 }; _5 < 4; ++_5)
{
for (uint _6{ 0 }; _6 < 4; ++_6)
{
for (uint _7{ 0 }; _7 < 4; ++_7)
{
for (uint _8{ 0 }; _8 < 4; ++_8)
{
compute(std::array<uint, 8> { _1, _2, _3, _4, _5, _6, _7, _8 });
}
}
}
}
}
}
}
}
}
int main() {
iter();
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Eine Lösung:
1 + 2 + 3 + 4 + 5 × 6 – 7 + 8 + 9 = 100.
Es gibt 9 Lösungen.
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung: