|
-
Jul 30th, 2001, 06:20 AM
#1
Thread Starter
New Member
Magic Squares
Recentallly i was doing a programming competition and this was one of the questions:
Task 4 - Balanced Squares
Description
9 numbers arranged in a 3x3 square is said to form a Balanced Square if every row and every column has the same sum.
For example
1 8 6
10 5 0
4 2 9
is a Balanced Square as every row and column add up to 15.
1 2 3
3 1 2
2 3 1
is also a Balanced Square, every row and column add up to 6.
Write a program which, given a list of 9 integers, prints them out in a Balanced Square. If the integers can be arranged into a Balanced Square in more than one way you may print whichever one you wish.
If it is not possible to form a Balanced Square using the integers print out a message stating this.
The format of your output should match the examples below:
Sample Output
Input
0 1 2 4 6 8 5 10 9
Output
1 8 6
10 5 0
4 2 9
Input
1 1 1 2 2 2 3 3 3
Output
1 2 3
3 1 2
2 3 1
Input
0 1 2 4 6 8 5 3 9
Output
0 1 2 4 6 8 5 3 9 cannot be made into a Balanced Square.
Test Data
0 1 2 4 6 8 5 10 9
1 1 1 2 2 2 3 3 3
0 1 2 4 6 8 5 3 9
1 2 3 4 5 6 7 8 9
12 1 2 3 4 5 6 7 8
1 2 3 3 6 7 4 5 5
8 2 7 1 4 3 3 6 2
Constraints
You may assume the integers are not negative.
It was the only question that I couldn't successfully complete, or even work out any way to even start it! All i could think of was to do a pure number crunch, but surly theres a better way! Any suggestions?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|