/
carremag.pas
178 lines (163 loc) · 3.35 KB
/
carremag.pas
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{ALGORITHME CarreMagique
//BUT: Construit un carré magique de dimension impaire
//ENTREE: Rien
//SORTIE: Carré magique
CONST TAILLE <- 5 : ENTIER
Procedure initialisation(carre : Tableau[1..TAILLE,1..TAILLE] de ENTIER)
//BUT: Initialise le carré magique
//ENTREE: Le tableau
//SORTIE: Rien
VAR i,j:ENTIER;
DEBUT
POUR i DE 1 A TAILLE FAIRE
POUR j DE 1 A TAILLE FAIRE
carre[i,j] <- 0;
FINPOUR
FINPOUR
FIN
Procedure affiche(carre : Tableau[1..TAILLE,1..TAILLE] de ENTIER)
//BUT: Affiche le tableau
//ENTREE: Carré magique
//SORTIE: Rien
VAR i,j:ENTIER
DEBUT
POUR i DE 1 A TAILLE FAIRE
POUR j DE 1 A TAILLE FAIRE
ECRIRE carre[i,j]
FINPOUR
ECRIRE //Saut de ligne
FINPOUR
FIN
Procedure deplace(i,j,jmax : ENTIER)
//BUT: Positionne sur la bonne cellule i,j
//ENTREE: i et j
//SORTIE: Rien
VAR jdeplace : ENTIER
DEBUT
SI (jmax = TAILLE) ALORS
jdeplace <- 1
SINON
jdeplace <- -1
FINSI
i <- i-1
j <- j+jdeplace
SI (j = jmax+jdeplace) ALORS
j <- TAILLE - jmax + jdeplace
FINSI
SI (i = 0 ) ALORS
i <- TAILLE
FINSI
FIN
procedure magique (var i,j:integer;var carre:IntegerArray);
//BUT: Remplir le tableau de nombres
//ENTREE: i et j pour se positionner, carre le tableau
//SORTIE: Rien
VAR k:ENTIER
DEBUT
POUR K DE 1 A (TAILLE*TAILLE)-1 FAIRE
deplace(i,j,TAILLE)
SI (carre[i,j] <> 0) ALORS
carre[i,j] <- k+1
SINON
TANTQUE (carre[i,j] <> 0) FAIRE
deplace(i,j,1)
FINTANTQUE
carre[i,j] <- k+1
FINSI
FINPOUR
FIN
VAR carre : Tableau[1..TAILLE,1..TAILLE] de ENTIER
i,j,k:ENTIER
DEBUT
initialisation(carre)
i <- (TAILLE DIV 2)
j <- (TAILLE DIV 2)+1
carre[i,j] <- 1
magique(i,j,carre)
affiche(carre)
FIN}
program CarreMagique;
//BUT: Construit un carré magique de dimension impaire
//ENTREE: Rien
//SORTIE: Carré magique
uses crt;
CONST TAILLE= 7;
Type IntegerArray = array[1..TAILLE,1..TAILLE] of integer;
procedure initialisation (var carre:IntegerArray);
//BUT: Initialise le carré magique
//ENTREE: Le tableau
//SORTIE: Rien
VAR i,j:integer;
begin
for i:=1 to TAILLE do
for j:=1 to TAILLE do
carre[i,j]:= 0;
end;
procedure affiche(var carre:IntegerArray);
//BUT: Affiche le tableau
//ENTREE: Carré magique
//SORTIE: Rien
VAR i,j:integer;
begin
for i:= 1 to TAILLE do
begin
for j:= 1 to TAILLE do
begin
if (carre[i,j] < 10) then
write(carre[i,j],' ')
else
write(carre[i,j],' ');
end;
writeln();
end;
end;
procedure deplace(var i,j : integer;jmax:integer);
//BUT: Positionne sur la bonne cellule i,j
//ENTREE: i et j
//SORTIE: Rien
VAR jdeplace:integer;
begin
if (jmax = TAILLE) then
jdeplace:=1
else
jdeplace:=-1;
i:=i-1;
j:=j+jdeplace;
if (j = jmax+jdeplace) then
j:= TAILLE - jmax + abs(jdeplace);
if (i = 0) then
i:= TAILLE;
end;
procedure magique (var i,j:integer;var carre:IntegerArray);
//BUT: Remplir le tableau de nombres
//ENTREE: i et j pour se positionner, carre le tableau
//SORTIE: Rien
VAR k:integer;
begin
for K:=1 to ((TAILLE*TAILLE)-1) do
begin
deplace(i,j,TAILLE);
if (carre[i,j] = 0) then
carre[i,j]:= k+1
else
begin
while (carre[i,j] <> 0) do
begin
deplace(i,j,1);
end;
carre[i,j]:= k+1;
end;
end;
end;
VAR carre : IntegerArray;
i,j:integer;
Begin
clrscr;
initialisation(carre);
i:= (TAILLE DIV 2);
j:= (TAILLE DIV 2)+1;
carre[i,j]:= 1;
magique(i,j,carre);
affiche(carre);
readln;
END.