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, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

Commenti


bottom of page