-
Notifications
You must be signed in to change notification settings - Fork 2
/
ms.cpp
executable file
·64 lines (54 loc) · 836 Bytes
/
ms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Magic square using recursion
#include<iostream>
using namespace std;
int check(int A[][10],int n)
{
int sumr=0,sumc=0,m=n*n;m+=1;m/=2;m*=n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sumr+=A[i][j];
sumc+=A[j][i];
}
if(sumc!=m || sumr!=m)
return 0;
}
return 1;
}
void print(int A[][10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<A[i][j]<<"###";
}
cout<<endl;
}
cout<<endl<<"-----------"<<endl;
}
void square(int A[][10],int n,int i,int j)
{
if(i==n-1 && j==n-1) {cout<<"square ends"<<endl;return;}
if(j==n-1 && i!=n-1)
{
cout<<"At"<<i<<","<<j<<endl;
j=0;i+=1;
square(A,n,i,j);
}
else
{
for(int l=1;l<=(n*n);l++)
{
A[i][j]=l;
if(A[i][j]==(n*n))j++;print(A,n);
square(A,n,i,j);
}
}
}
int main()
{
int A[10][10];
square(A,3,0,0);
}