CCC '16 J2 - Magic Squares

View as PDF

Submit solution

Points: 3
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Magic Squares are square arrays of numbers that have the interesting property that the numbers in each column, and in each row, all add up to the same total.

Given a 4 \times 4 square of numbers, determine if it is magic square.

Input Specification

The input consists of four lines, each line having 4 space-separated integers.

Output Specification

Output either magic if the input is a magic square, or not magic if the input is not a magic square.

Sample Input 1

16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Output for Sample Input 1


Explanation for Output for Sample Input 1

Notice that each row adds up to 34, and each column also adds up to 34.

Sample Input 2

5 10 1 3
10 4 2 3
1 2 8 5
3 3 5 0

Output for Sample Input 2

not magic

Explanation for Output for Sample Input 2

Notice that the top row adds up to 19, but the rightmost column adds up to 11.


  • 1
    Winbigwok  commented on March 23, 2020, 9:40 p.m.

    test cases too weak just need to check rows

  • 0
    Richard_Ni  commented on Feb. 23, 2020, 2:04 p.m.

    when there is one single "." in the wrong place //error//error

  • 1
    2405873453  commented on Jan. 15, 2020, 12:03 a.m.

    Everyone, if you are using java to solve this question,and having a wa because of "presentation",remember to use System.out.println instead of just print, the grader has some little problem because it is tesing all the case together, so if your out put don't move to the nxt line, what domj grader will see is something like"magicmagicnot magicmagic"

  • -10
    RyanBadBoy  commented on Nov. 24, 2019, 8:01 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

  • -31
    Tim2004  commented on Oct. 4, 2019, 11:49 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

  • 14
    Plasmatic  commented on March 4, 2019, 9:43 a.m. edited

    this must be the predecessor to what is probably the most controversial CCC problem of all time

  • 23
    ItzMatthew  commented on Oct. 6, 2018, 2:00 p.m.

    I was working on the code for this, and my brother said that I needed to include that diagonals need to all add up as well, so my code failed like 10 times until I finally figured out that he lied to me. To publicly shame him, his name is loltrollkill

  • 2
    Y_Yousfi  commented on May 1, 2018, 6:41 p.m.

    on the first case when i use my code it outputs "magic" i get WA. i use another code that outputs "not magic" on purpose and it still gives WA. why is that?

    • 8
      Roynaruto  commented on May 2, 2018, 12:33 p.m.

      Adding a \n after magic and not magic output causes it to work in your second most recent submission.

      • 2
        Ethan_Pronev  commented on Sept. 16, 2018, 2:09 p.m.

        I was having the same problem and after adding \n it worked. Is there a reason why this is necessary?

      • 0
        owenzhu2012  commented on May 31, 2018, 7:06 p.m.

        Damn that helped, why is that tho?

        • 1
          Korjwist  commented on Jan. 12, 2020, 2:52 p.m. edit 2

          Shouldn't this problem be fixed?? I spent like 30 minutes trying to figure out what my problem was until I came to the comment section...

  • 0
    KenzoEngineer  commented on March 14, 2018, 7:01 p.m.

    I'm getting a NumberFormatException in my code for some of the batches. It works in eclipse and netbeans, not sure if there is some weird case I'm not accounting for.

  • -1
    Roronoa_Zoro1540  commented on Aug. 10, 2017, 12:04 p.m.

    Jesus I thought the diagonals would add up too so I outputted "not magic" for one since the diagonals didn't add up lmao

  • -41
    340975960  commented on May 15, 2017, 5:06 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

  • 3
    BRussell  commented on Nov. 5, 2016, 9:39 p.m.

    I keep receiving a number format exception in a few of the test cases. Am I not to assume that the inputs will valid? Or do I have a misconception of a NumberFormatException? I was pretty sure it was just when a String was attempted to be converted into a numeric value.

    • 1
      Kirito  commented on Nov. 6, 2016, 1:06 a.m. edit 3

      Edit: Fixed.