Magisches Quadrat ()
2 Kommentare
4 Lösung(en)
m = [
[23, 4, 18, 1, 19],
[5, 10, 15, 14, 21],
[6, 17, 13, 9, 20],
[24, 12, 11, 16, 2],
[7, 22, 8, 25, 3],
]
def is_magic_square(matrix):
if len(matrix) <= 0 or any(len(matrix) != len(row) for row in matrix):
return False
expected_sum = sum(matrix[0])
for row in range(1, len(matrix)):
if sum(matrix[row]) != expected_sum:
return False
for column in range(0, len(matrix)):
if sum(row[column] for row in matrix) != expected_sum:
return False
d1, d2 = 0, 0
for n in range(0, len(matrix)):
d1 += matrix[n][n]
d2 += matrix[n][len(matrix)-n-1]
return d1 == expected_sum and d2 == expected_sum
print(is_magic_square(m)) # True
Lösung von: Name nicht veröffentlicht
Array.prototype.sum = function() { return eval(this.join('+')); }
function isMagicSquare(matrix) {
let testSum = matrix[0].sum(),
len = matrix.length,
x, y, tmp = [];
// zahlen im richtigen bereich?
for (x = 0; x < len; x++) tmp = tmp.concat(matrix[x]);
tmp.sort(function(a, b) {return a > b});
if (tmp[0] != 1 || tmp.length != len**2) return false;
for (x = 0; x < tmp.length-1; x++)
if (tmp[x+1] != tmp[x]+1) return false;
// zeilen
for (x = 1; x < len; x++)
if (matrix[x].sum() != testSum) return false;
// spalten
for (x = 0; x < len; x++) {
tmp = [];
for (y = 0; y < len; y++) tmp.push(matrix[x][y]);
if (tmp.sum() != testSum) return false;
}
// diagonalen
tmp = [[], []];
x = len-1; y = 0;
while (y < len) {
tmp[0].push(matrix[x][y]);
tmp[1].push(matrix[y][x]);
x--; y++;
}
if (tmp[0].sum() != testSum || tmp[1].sum() != testSum) return false;
return testSum;
}
let mySquare = [
[23, 4, 18, 1, 19],
[ 5, 10, 15, 14, 21],
[ 6, 17, 13, 9, 20],
[24, 12, 11, 16, 2],
[ 7, 22, 8, 25, 3]
];
console.log( isMagicSquare(mySquare) );
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 5.x; C# 9.x; VS-2019
using System;
using System.Collections.Generic;
using System.Linq;
List<List<int>> _lst = new()
{
new() { 23, 4, 18, 1, 19 },
new() { 5, 10, 15, 14, 21 },
new() { 6, 17, 13, 9, 20 },
new() { 24, 12, 11, 16, 2 },
new() { 7, 22, 8, 25, 3}
};
var _sum = _lst[0].Sum();
Console.WriteLine(IsMagicSquare(_lst));
(bool, int?) IsMagicSquare(List<List<int>> lst)
{
// alle Zahlen vorhanden
if (!lst.SelectMany(x => x).OrderBy(x => x).SequenceEqual(Enumerable.Range(1, (int)Math.Pow(lst.Count, 2)))) return (false, null);
// Zeilen
if(!lst.All(x => x.Sum() == _sum)) return (false, null);
// Spalten (transponieren)
if(!lst.SelectMany(inner => inner.Select((item, index) => new { item, index })).GroupBy(i => i.index, i => i.item).Select(x => x.ToList()).All(x => x.Sum() == _sum)) return (false, null);
// Diagonalen
var dia = Enumerable.Range(0, lst.Count).Select(x => new { first = lst[x][x], second = lst[x][^(x+1)] }).ToList();
if(_sum != dia.Select(x => x.first).Sum() || _sum != dia.Select(x => x.second).Sum()) return (false, null);
return (true, _sum);
}
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
const std::vector<std::vector<int>> msqr {
{ 23, 4, 18, 1, 19 },
{ 5, 10, 15, 14, 21 },
{ 6, 17, 13, 9, 20 },
{ 24, 12, 11, 16, 2 },
{ 7, 22, 8, 25, 3 } };
int sum_of_row(const std::vector<int>& v) {
return std::accumulate(v.begin(), v.end(), 0);
}
const auto EXP{ sum_of_row(msqr[0]) };
bool check_numbers() {
std::vector<int> nums{}, comp{};
for (const auto& r : msqr)
for (const auto& c : r)
nums.push_back(c);
std::sort(nums.begin(), nums.end());
for (auto i{ 1 }; i <= nums.size(); i++)
comp.push_back(i);
return nums == comp;
}
bool check_diagonals() {
std::vector<int> fst{}, sec{};
for (auto i{ 0 }; i < msqr[0].size(); i++) {
fst.push_back(msqr[i][i]);
sec.push_back(msqr[i][msqr[0].size() - i - 1]);
}
return sum_of_row(fst) == EXP and sum_of_row(sec) == EXP;
}
bool check_rows(std::vector<std::vector<int>> v = msqr) {
for (const auto& r : v)
if (sum_of_row(r) != EXP)
return false;
return true;
}
bool check_columns() {
for (auto i{ 0 }; i < msqr[0].size(); i++) {
std::vector<int> v;
for (auto k{ 0 }; k < msqr[0].size(); k++)
v.push_back(msqr[k][i]);
if (sum_of_row(v) != EXP) return false;
}
return true;
}
int main() {
const auto chk{ check_numbers() && check_diagonals() && check_rows() && check_columns()};
std::cout << "it's "<< (chk ? "" : "not ") << "a magic square" << "\n";
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | k.A. |
Webcode: | hq6g-root |
Autor: | () |
Kommentare (2)
Find ich gut. Bitte lösche auch
https://www.programmieraufgaben.ch/aufgabe/tuerme-von-hanoi-graphisch-java/67gfirgd
https://www.programmieraufgaben.ch/aufgabe/neko-im-labyrinth-java-1/zf6tg2nw
und
https://www.programmieraufgaben.ch/aufgabe/planetensortieren-java/6gqw5zue
gleich mit.
Bitte ändern (ansonsten wird die Aufgabe vom Admin gelöscht)!
Die Programmiersprache darf nicht vorgegeben werden! Zudem ist dem Autor wohl entgangen, dass nicht eine Aufgabe, sondern ein Bild hochgeladen wurde. Dies ist technisch nicht weiter verarbeitbar.