do magic square
Time limit:1000ms Memory limit:1024k
Description:apple recently fascinated to do magic side, Apple or a master, as long as you say an odd number of N, he can put n*n magic side. In fact, you can do better than him. Apple is always a mess, and you can use the program neatly. ^_^ Magic side requirements: Each row, each column, and two diagonal lines on the number and are equal.
Input: Each line has a number n (0< N < 30) and the input 0 ends. Output: Input an odd number, outputting a magic square, each of the 3 block, the order of reference to the template output, after the output plus a carriage return. Sample Input:
510
Sample Output:
2 9 (3) 4 6 (5 7), 1 8, 1
Hint: If you don't do magic, count from 1 to the end, and believe you'll find the rules. Of course the output should also follow this format. Just beginning to write is from the center began to pick the writing, in fact, with the computer's thinking to think, in order to look good. Then today more dizzy, row and column is not clear, because this reason wrote for a long time.
1#include <iostream>2#include <iomanip>3 using namespacestd;4 intMain ()5 {6 intN;7 while(cin>>N)8 {9 if(n==0)Ten Break; One int**a=New int*[n]; A for(intI=0; i<n;i++) -a[i]=New int[n]; - for(intI=0; i<n;i++) the for(intj=0; j<n;j++) -a[i][j]=0; - - intC=1; + intJ= (n1)/2; - for(inti=n-1; i>=0; i++) + { A if(c!=1&& C-1)%n==0) at { - if(j==0) -j=n-1; - Elsej=j-1; - if(i==0) -i=n-2; in Else if(i==1) -i=n-1; to Elsei=i-2; + } -A[i][j]=c++; theJ + +; * if(c==n*n+1) Break; $ if(i==n-1)Panax Notoginsengi=-1; - if(j==N) thej=0; + } A for(intI=0; i<n;i++) the { + for(intj=0; j<n;j++) -COUT<<SETW (3) <<A[i][j]; $cout<<Endl; $ } -cout<<Endl; - } the return 0; -}
View Code
Problem Solving report 1005 magic Square