top of page

CCC 2016 J2: Magic Squares


Turn input into a matrix(2D array). Then calculate the sum of rows and columns and compare them. If they are all equal output "magic" else output "not magic"


square = []
sums_row = []
sums_col = []
for i in range(4):
    currentrow = [0,0,0,0]
    square.append(currentrow)
for i in range(4):
    currentline = input().split()
    for j in range(4):
        square[i][j] = int(currentline[j])
for i in range(4):
    csum = 0
    for j in range(4):
        csum += square[i][j]
    sums_row.append(csum)

for i in range(4):
    csum = 0
    for j in range(4):
        csum += square[j][i]
    sums_col.append(csum)

if sums_col[0] == sums_col[1] and sums_col[1] == sums_col[2] and sums_col[2] == sums_col[3]\
   and  sums_row[0] == sums_row[1] and  sums_row[1] == sums_row[2] and  sums_row[2] == sums_row[3]\
   and sums_col[0] == sums_row[0]:
   print("magic")
else:
    print("not magic")


Recent Posts

See All

CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c,...

CCC '24 J4 - Troublesome Keys

s1 = input() s2 = input() silly = '' silly_o = '' quiete = '-' i = 0 j = 0 while i < len(s1) and j < len(s2): if s1[i] != s2[j]: if...

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int,...

Comments


bottom of page