,
,
,
. ,
,
k . 1- :
(A[i])
= ([0]) + i * k
MXN, ,
[i, j ] :
([i,j])
= ([0,0]) + (i * N + j) * k
,
, .
, (QBasic)
(, ) ,
. ,
.
,
, .
,
, .
,
. , ,
.
QBasic
DIM:
DIM (10),(2
8,3 5),(3,2,6)
,
0, , , 10, 11 .
"qq kk" ,
.
QBasic ,
. :
INPUT "
F", N DIM F(N)
:
INPUT "
F",M REDIM F(M)
, .
QBasic . DIM
,
.
,
"" . , ,
.
,
.
- .
:
#define N_max
50
char a1[20], a2[5][80];
int b1[25],b2[N_max];
0, , , b1
b1[0], b1[1], ..., b1[24]. b1[25]
.
. ,
f, q k ( q k
),
:
type
mat_q_k=array
[l..q,l..k] of integer;
var
f :mat_q__k;
var
f:array [l..q,l..k] of integer;
var
f:array [l..q][l..k] of integer;
var
f:array [l..q]
of array [l..k] of integer;
,
,
. , :
var
ch:array ['A'..'Z'] of integer;
str:string;
begin
.....................
inc(ch[str[j]])
;
....................
.
""
. ,
(, , ).
.
:
char [7]="";
char b[7]={'','','','','','',0x00};
char []="";
int d[10] = {1,2,3,4};
int f[2][3]={{1,2,3},
{4,5,6}};
,
.
[6] .
, ,
. ,
.
.
7 , , .
const: const
a:string[7]='';
d:array [1..10]
of integer=(1,2,3,4);
f:array [1..2,1..3]
of integer=((1,2,3),(4,5,6));
.
.
,
,
.
QBasic
,
. ,
. ,
. .
,
.
.
.
QBasic ,
. ,
$STATIC REM $DYNAMIC,
, . ERASE
v1,v2, ... "",
.
"" ()
. REDIM
,
. .
:
q=(_q *)calloc(n_el,s_el);
// ;
q=( q *)farcalloc(n_el,s_el);
// ;
q=(_q *)malloc(n_byte);
// ""
q=(_q *)farmalloc(n_byte);
// ""
q new=realloc(q_old,n_byte);
//
q_new=farrealloc(q_old,n_byte);
//
free(q); //
farfree(q);
//
q ,
. n_el s_el
. n_byte
.
, "", 65
521 . far ,
far huge, ""
64 .
"" (NULL)
.
,
q.
realloc
(farreallioc) .
, .
, ,
q_old, .
Borland C++
,
:
q=new
[n_1]; // n_e1 ;
q=new ; //
;
delete q[n_e1];
// , ;
delete q; //
,
//
;.
New:
type
mas=array [1..200]
of integer;
point=^mas;
var
p:point;
j:integer;
..................
begin New(p);
for j :=1 to 200 do
readln(p^[j]);
...................
Dispose ().
GetMem(q,n_byte)
FreeMem(q,n_byte). ,
, (q:pointer;).
,
, , ""
.
. "
"
:
q=NULL; //
p:=nil; //
,
. , ,
,
.
,
, ,
.
:
SUBROUTINE MATMULT(A,B,C,N)
REAL A(N,N),B(N,N),C(N,N),D
DO 1 J=1,N
DO 1 K=1,N D=0
DO 2 L=1,N
2 D=D+A(J,L)*B(L,K)
1 C(J,K)=D
END
, .
.
-
QBasic
QBasic.
.
:
4_01.bas
DECLARE SUB
ADDMAT(A%(),B%(),C%(),N%) DEFINT A-Z CLS
DIM A1(2,2),
A2(2,2),A3{2,2)
DIM Bl(3,3),B2(3,3),B3(3,3)
FOR J=0 TO 2
: FOR K=0 TO 2
A1(J,K)=J+K
: A2(J,K)=J*K
NEXT : NEXT
J
CALL ADDMAT(Al(),A2(),A3() ,2) '
FOR J=0 TO 3 : FOR K=0 TO 3 B1(J,K)=J+K :
B2(J,K)=J*K
NEXT : NEXT
J
ADDMAT Bl(),B2(),B3(),3
' END
SUB ADDMAT (A%(),%(),%(),N%)
DEFINT A-Z
FOR Q=0 TO N : FOR S=0 N
C(Q,S)=A(Q,S)+B(Q,S)
NEXT S : NEXT Q
END SUB
, -
, ,
. DIM A(N,N)
. , ,
.
, %, B%, C%, N%,
, , , N.
DEFINT A-Z,
.
,
.
-
,
,
, .
, :
function Max(a:array
[1..100] of integer):integer;
:
type
mas100=array
[1..100] of integer;
function Max(a:mas100):integer;
masioo.
? ,
:
function Max(a:mas100;
n:integer):integer;
n ,
mas100.
,
:
function Max(var a; n:integer):integer;
type
b=array [1.. Maxlnt] of integer;
var
m,k:integer;
begin
m:=b(a}[1];
for k:=2 to
n do
if m<b(a)[k]
then m:=b(a)[k] ;
Max:=m;
end;
b() ,
, .
" ".
"b". ,
n (
, ).
, Maxint,
64 .
,
:
function Max(var a; n:integer):integer;
var
c:array[l..
Maxlnt] of integer absolute a;
m,k:integer;
begin
m:=c[l];
for k:=2 to
n do
if m<c[k] then m:=c[k];
Max:=m;
end;
absolute , ,
, , ,
.
, :
function Max(pa:pointer; n:integer):integer;
var
m,k:integer;
pb:^integer;
begin
pb:=ptr((^)
, ofs(^) ) ;
m:=b^;
for k:=2 to
n do
begin
pb:=ptr(seg(^),ofs(^)+k*2);
if m < b^
then m:=b^;
end;
Max:=m;
end;
q
:
max_q:=Max(&q,100);
.
ptr, (seg)
(ofs). , ,
a (seg(pb^)=seg(pa^)),
ofs (^)
2*k.
,
,
.
, 0.
:
function Max(a:array of integer):integer;
var
m,k:integer;
begin
m:=a[0];
for k:=l to
High(a) do
if m < a[k]
then m:=a[k];
Max:=m; end;
High, .
{$P+}.
,
. -,
. -, ,
.
. :
procedure sum+mat(var a,b,c; n:integer);
var
x:array [0..0]
of integer absolute a;
y:array [0..0]
of integer absolute b;
z:array [0..0]
of integer absolute c;
j,k:integer;
begin {$R-}
for j :=0 to
n-1 do
for k:=0 to
n-1 do
z[j*n+k]:=x[j*n+k]+y[j*n+k];
{$R+} end;
, ,
.
0 0
.
:
procedure sum_mat(pa, pb, pc:pointer; n:integer);
var
a,b,:^integer;
j,k:integer;
begin
for j:=0 to n-1 do
for k:=0 to n-1 do
begin
a:=ptr(seg(^),ofs(^)+(j*n+k)*2)
;
b:=ptr(seg(pb^),ofs(b^)+(j*n+k)*2);
c:=ptr(seg(^),ofs(^)+(j*n+k)*2);
^:=^+b^;
end;
end;
-
. ,
.
,
.
, .
int Max(int
*a, int n) {
int m, k;
m=a[0];
for(k=l; k<n;
k++)
if (m < a[k])
m=a[k];
return m; }
,
High .
.
:
int Max(int
*a, int n) {
int m,k;
m=*a;
for(k=l; k<n;
k++)
if (m < *(a+k) m=*(a+k);
return m; }
:
void sum_mat(int
*a, int *b, int *c, int n) {
int j,k,m;
for(j=0; j < n; j++)
for(k=0; k <
n; k++) {
m=j*n+k;
* (c+m)=* (a+m)
+ *(b+m) ; } }
,
, 90% .
,
10% .
.
,
, . .
. [12],
.
. "
", . 3.
(bubble)
,
( ) .
.
. .
(bubble1),
, ,
. ,
.
i j
.
bubble.bas
SUB BUBBLE(X(),N)
FOR 1=1 TO N-l
FOR J=N-1 TO I STEP -1
IF X(J-l)
> X(J) THEN
TMP=X(J-1): X(J-l) =X(J): X(J)=TMP
END IF
NEXT J
NEXT I
END SUB
bubbiei.bas
SUB BUBBLE1(X(),N)
M:Q=0
FOR 1=1 TO N-l
IF X(I-l) >
X(I) THEN
TMP=X(I-1): X(I-1)=X(I): X(I)=TMP: Q=l
END IF
NEXT I
IF Q=l THEN GOTO *M
END SUB
bubble.
void bubble(int
*x, int n) {
register int
i,j;
int tmp;
for(i=l; i<n;
i++)
for(j=n-l; j>=i; j)
if<x[j-l]
> x[j])
{
tmp=x[j-1] ;
x[j-l] = x[j];
x[j ] = tmp;
} }
bubblel.c
void bubblel(int
*x, int n) {
register int i,j;
int tmp,q; m:q=0;
for(i=0; i<n-l; i++)
if(x[i] >
x[i+l]) {
tmp=x[i];
x[i]=x[i+l];
x[i+l]=tmp;
q=l; }
if(q!=0) goto
m; }
bubble.pas
procedure bubble(var x:array of integer;n:integer);
var
i,j,tmp:integer;
begin
for i:=l to
n-1 do
for j:=n-l downto i do
if x[j]<x[j-l]
then begin
tmp:=x[j-l]; x[j-l]:=x[j];
x[j]:=tmp;
end;
end;
bubblel.pas
procedure bubblel(var
x:array of integer;n:integer);
label m;
var
i,tmp,q:integer;
begin m:q:=0
for i:=0 to
n-2 do
if x[i] >
x[i+l] then begin
tmp:=x[i];
x[i]:=x[i+l];
x [i+1] :=tmp;
q:=l; end;
if q=l then goto m;
end;
(select)
.
. ,
, . .
select.bas
SUB SELECT(X(),N)
FOR I=0 TO N-2
Q=0: K=I: TMP=X(I)
FOR J=I+1 TO
N-l
IF X(J)<TMP
THEN K=J: TMP=X(J): Q=l NEXT J
IF Q=l THEN X(K)=X(I): X(I)=TMP NEXT I
END SUB
select.
void select(int
*x, int n) {
register int i,j,k;
int q,tmp;
for(i=0; i<n-l;
i++) {
q=0;
k=i;
tmp=x[ i ] ;
for(j=i+l; j<n;
j++) {
if(x[j] <
tmp) {
k=j;
tmp=x[ j ] ;
q=l; } } if
(q)
{x[k]=x[i];
x[i]=tmp;} }
}
select.pas
procedure select(var x:array of integer;n:integer);
var
i,j,k,q,tmp:integer;
begin
for i:=0 to
n-2 do begin
q:=0;
k:=i;
tmp:=x[i];
for j:=i+l to n-1 do
if x[j]<tmp
then begin
k:=j;
tmp:=x[j];
q:=l;
end;
if q=l then
begin
x[k]:=x[i];
x[i]:=tmp;
end;
end;
end;
(insert)
.
.
. ,
. ,
n-1 .
.
insert.bas
SUB INSERT(X%(),N%) DIM I,J,TMP
FOR I=1 TO N-l TMP=X(I)
FOR J=I-1 TO
0 STEP -1
IF TMP > X(J) THEN EXIT FOR
X(J+1)=X(J)
NEXT X(J+1)=TMP
NEXT I
END SUB
insert.c
void insert(int
*x,int n) {
register int
i,j; int trnp;
for(i=l; i<n;
i++) {
tmp=x[ i ] ;
for(j=i-l;j>=0
&& tmp < x[j]; j--)
x[j+l]=x[j];
x[j+l]=tmp;
} }
insert.pas
procedure insert(var x:array of integer;n:integer);
var
i,j,tmp:integer;
begin
for i:=l to n-1 do
begin
tmp:=x[i];
j:=i-l;
while (j>=0)
and (tmp<x[j]> do
begin .
x[j+l]:=x[j]; j:=j-l;
end;
x [ j+1] :=tmp;
end;
end;
(shell)
.
. , 1959 .,
. ,
, .
, , .
, , .
.
9>5>3>2>1.
shell.bas
SUB SHELLSORT (X%"() , N% )
DIM I, J,GAP,K,XX,A(5) A(0)=9: A(l)=5: A(2)=3: A(3)=2: A(4)=l
FOR K=0 TO 4
GAP=A(K)
FOR I=GAP TO N-1 XX=X(I)
FOR J=I-GAP TO
0 STEP -GAP
IF XX >= X(J) THEN EXIT FOR
X(J+GAP)=X(J)
NEXT J X(J+GAP)=XX
NEXT I
NEXT
END SUB
shell.
void shell(int
*x,int n) {
register int
i,j,gap,k; int xx;
char a[5]={9,5,3,2,l};
for(k=0; k<5;
k++) {
gap=a[k];
for(i=gap; i<n;
++i) {
xx=x[i];
for(j=i-gap;
xx < x[j] && j >= 0; j=j-gap)
x[j+gap]=x[j);
x[j+gap]=xx; } } }
shell.pas
procedure shell(var x:array of integer;n:integer) ;
var
i,j,k,gap,xx:integer;
const
a:array [0..4] of byte=(9,5,3,2,1};
begin
for k:=0 to
4 do
begin
gap:=a[k];
for i:=gap to
n-1 do begin
xx:=x[i];
j:=i-gap;
while (j >=
0) and (xx < x[j]) do
begin
x[j+gap]:=x[j];
j:=j-gap;
end;
x[j+gap]:=xx;
end;
end;
end;
(quicksort)
.
. . , 1962 .,
" ".
, .
, , ,
. ,
. ,
" ".
. -
(
). ( ,
), , , , ,
, .
, ,
, ,
.
, , .
.
.
,
.
hoar, ,
quick. ,
left right.
hoare.bas
SUB HOARE(X%
(),N%)
QUICK X%(),0,N%-1
END SUB
SUB QUICK(X%(),LEFT%,RIGHT%)
DIM I,J,XX,YY
I=LEFT%: J=RIGHT%
XX=X((LEFT%+RIGHT%)\2)
DO
WHILE X%(I) < XX AND I < RIGHT%: I=I+1: WEND
WHILE XX < X%(J) AND J > LEFT%: J=J-1: WEND
IF I <= J
THEN
YY=X%(I): X%(I)=X%(J): X%(J)=YY: 1=1+1: J=J-1
END IF
LOOP WHILE I
<= J
IF LEFT% < J THEN QUICK X%(),LEFT%,J
IF I < RIGHT% THEN QUICK X%(),I,RIGHT%
END SUB
hoare.c
void hoare(int
*x,int n) {
quick(x,0,n-1);
return;
}
/*--------------------------------*/
void quick(int
*x, int left, int right) {
register int
i,j;
int xx,tmp;
i=left;
j=right;
xx=x[(left+right)/2];
do
{
while (x[i]
< xx && i < right) i++;
while (xx <
x[j] && j > left) j--;
if (i<=j)
{
tmp=x[ i ] ;
x[i]=x[j];
x[j]=tmp;
i++; j-- ;
}
}
while(i<=j);
if(left < j) quick(x,left,j);
if(i < right) quick(x,i,right);
}
hoare.pas
procedure quick(var x:array of integer;left,right:integer);
var
i,j,xx,tmp:integer;
begin
i:=left; j:=right;
xx:=x[(left+right)
div 2];
repeat
while (x[i] < xx) and (i < right) do i:=i+l;
while (xx < x[j]) and (j > left) do j:=j-l;
if i<=j then
begin
tmp:=x[i];
x[i]:=x [j];
x[j]:=tmp;
i:=i+l;
j:=j-i;
end;
until i>j;
if left < j then quick(x,left,j);
if i < right then quick(x,i,right);
end;
procedure hoare(var x:array of integer;n:integer);
begin
quick(x,0,n-l);
end;
.
. . ,
.
. ,
(, , ), ,
q.
() j (a[j] = q).
,
.
, q = [ 1 ].
.
/2.
:
,
. . .
,
( S2 S4), .
, . . -
1520%.
, ,
q. ,
[ j ], q, j < n + 1 .
, ,
j n + l. .
,
, ,
. ,
(j 1 n), (j n i) .
LOOP,
1 .
, 0. ,
(j < n + 1) .
, 1,
.
ssearch.bas
FUNCTION SSEARCH(Q,A(),N)
FOR J=0 TO N-l
IF Q=A(J) THEN
SSEARCH=J: EXIT FUNCTION
NEXT J
SSEARCH=-1
END FUNCTION
ssearch.c
int ssearch(int q, int *a, int n)
}
register int j;
for(j=0; j<n;
j++)
if(q==a[j]) return j;
return -1;
}
ssearch.pas
function ssearch(q:integer;a:array of integer;n:integer):integer;
var
j:integer; begin
for j:=0 to n-1 do
if q=a[j] then
begin
ssearch:=j;
exit;
end;
ssearch:=-l;
end;
, ,
.
,
, .
, g [[1],[n]].
left right ,
. left = 1 right = n. q
a [mid] (mid = (left + right)/2).
q a [mid],
mid - 1. q a [mid],
mid + 1. ,
q = a [mid], .
, left
right. ,
(right = left + l) :
a[left] <
q < a[right]
, q .
bsearch.bas
FUNCTION BSEARCH(Q,A()
,N) LEFT=0: RIGHT=N-1
WHILE LEFT <=
RIGHT
MIDDLE=(LEFT+RIGHT)\2
IF Q < A(MIDDLE) THEN RIGHT=MIDDLE-1: GOTO M
IF Q > A(MIDDLE) THEN LEFT=MIDDLE+1: GOTO M
BSERACH=MIDDLE
: EXIT FUNCTION M:WEND
BSEARCH=-1 END
FUNCTION
bsearch.c
int bsearch(int
q,int *a,int n)
{
register int left,right,mid;
left=0; right=n-l;
for(;left <= right;)
{
mid=(left+right)12;
if(q < a[mid]) right=mid-l;
else if(q > a[mid]) left=mid+l;
else
return mid; }
return -1; }
bsearch.pas
function bsearch(q:integer;a:array of integer;n:integer):integer;
var
left,right,mid:integer;
begin
left:=0; right:=n-l;
until left <= right do
begin
mid:=(left+right)
div 2;
if q < a[mid]
then right=mid-l
else if q >
a[mid] then left=mid+l
else
begin
bsearch:=mid;
exit;
end;
end;
bsearch:=-l;
end;
[O,N]. ,
, :
. , , . . N/2. , , . log2N .
[0,100], 7 .
Y ( )
, .
4_02.bas
'
[0,100]
DEFINT A-Z
CLS
LEFT=0: RIGHT=100:
DO
MIDDLE=(LEFT+RIGHT)\2
PRINT "
, "; MIDDLE; " (Y/N) - ";
INPUT "",A$
IF RIGHT-LEFT
<= 1 THEN
IF A$="Y"
OR A$="y" THEN
PRINT " ";RIGHT: END
ELSE
PRINT " ";LEFT: END
END IF
END IF
IF A$="Y" OR A$="y" THEN LEFT=MIDDLE+1 ELSE RIGHT=MIDDLE
LOOP
END
4_02.
/* [0,100] */
#include <stdio.h>
#include <conio.h>
main ()
{
int left=0,
right=100, middle, ok;
char ch;
clrscr();
for (;;)
{
middle={left+right)/2;
printf("\n
, % d (Y/N) - ", middle);
ch=getche();
if(right-left
<= 1)
if (ch=='Y'
|| ch == 'y') { ok=right; break; }
else { ok=left;
break;}
if(ch=='Y' II
ch== 'y') left=middle+l;
else right=middle;
}
printf("\
%d",ok); getch(); }
4_02.pas
{
[0,100] }
uses Crt;
var
ch: char;
left, right, middle, ok: byte;
begin
left:=0;
right:=100;
clrscr;
repeat
middle := (left
+ right) div 2;
write('
, ',middle,' (Y/N) - ');
ch:=readkey;
writeln(ch);
if (right-left
<= 1) then
if (ch='Y') or (ch = 'y') then begin ok:=right;
break;
end else begin ok:=left;
break;
end; if(ch='Y')
or (ch='y') then left := middle + 1
else right := middle;
until false;
writeln(' ',ok);
readln;
end.
4.03.
,
, . .
.
1 ()
,
.
/2 (n ),
. , ,
, .
4_03.bas
DECLARE SUB
INVERT (A%(), N%)
DEFINT A-Z
CLS
N = 20
DIM A(N)
PRINT "
:"
FOR I=0 N-l:
A(I)=I+1: PRINT A(I); : NEXT I
PRINT
INVERT A(),
N
PRINT "
:"
FOR I=0 N-l:
PRINT A(I); : NEXT I
END
SUB INVERT (A%(),
N%)
DEFINT A-Z
FOR I = 0 TO
(N-l)\2
TMP = A(I):
A(I) = A(N-I-l): A(N-I-1) = TMP
NEXT I
END SUB
4_03.
#include <stdio.h>
#include <conio.h>
void invert(int *a, int n) ;
void main(void)
{
#define N 20
int i,a[N];
clrscr ();
printf("
:\n");
for(i=0; i <
N; i++)
fa[i]=i+l; printf("%3d",a[i]);
}
invert(a,N);
printf("\n
:\n");
for(i=0; i < N; i++)
printf("%3d",a[i]);
getch(); } void
invert(int *a, int n)
int j,tmp;
for(j=0; j <
n/2; j++)
{ tmp=a[j];
a[j]=a[n-j-1];
a[n-j-l]=tmp;
} }
.
.
1 ()
""
( . / . . .: ,
1990). .
.
.
, .
,
,
. ,
.
, ()
.
i (i < i < k),
:
1-
k, 2- k-l,
k-
1.
, , k+l. 1-
k-i+1.
+1. ,
i (n+l) - (k-i+l)=n-k+i.
:
1-
n-k+l, 2-
n-k+2,
k-
n-k+k.
,
,
.
1- (k + i < i < n).
:
k+l , k+2
n-1,
k+l.
, , n+k+i. 1-
-
n+k+l-i.
,
n+1, :
k+l, n,
(n+l)-n=l;
k+2, -1,
(n+1)-(n-1)=2;
, k+l,
(n+l)-(k+l)=n-k.
,
, ,
.
2 ()
invert, , ,
. j,
, len , .
4_04.bas
DECLARE SUB
INVERT1 (A%(), J%, N%)
DEFINT A-Z
CLS
N=20: K=15
DIM A(N)
PRINT "
:"
FOR I=0 TO N-l:
A(I)=I+1: PRINT A(I); : NEXT I
PRINT
INVERT1 A(),0,K
PRINT "
:"
FOR I=0 N-l:
PRINT A(I); : NEXT I: PRINT
INVERT1 A(),K,N-K
PRINT "
:"
FOR I=0 N-l:
PRINT A(I); : NEXT I: PRINT
INVERT1 A(),0,N
PRINT "
:"
FOR I=0 TO N-l:
PRINT A(I); : NEXT I
END
SUB INVERT1
(A%(), J%, N%)
DEFINT A-Z
FOR I = J TO
J+(N-1)\2
TMP=A(I): A(I)=A(2*J+N-1-I):
A(2*J+N-l-I)=TMP
NEXT I
END SUB
4_04.
#include <stdio.h>
#include <conio.h>
void invertl(int
*a, int j , int k) ;
void main(void)
{
#define N 20
#define 15
int i,a[N];
clrscr () ;
printf("
:\n");
for(i=0; i <
N; 1++)
{
a[i] = 1 + 1;
printf("%3d",a[i]);
}
invertl(a,0,K);
printf("\n
:\n");
for(i=0; i < N; i++)
printf("%3d",a[i]);
invertl(a,K,N-K);
printf("\n
:\n");
for(i=0; i < N; i++)
printf("%3d",a[i]);
invertl(a,0,K);
printf("\n
:\n");
for(i=0; i < N; i++)
printf("%3d",a[i]);
getch(); }
void invertl(int
*a, int j, int k) {
int m,tmp;
for(m=j; m <
j+k/2; m++)
{
tmp=a[m]; a[m] = a[2*j+k-m-1];
a[2*j+k-m-l]=tmp;
}
}
4_04,pas
program pearl;
uses crt; const
N=20; K=15;
var
a:array [1..N] of integer;
i:integer;
procedure invertl(var a; j,k:integer);
var
m,tmp:integer;
aa:array [1..1]
of integer absolute a;
begin {$R-}
for m:=j to
j + k div 2 do begin
tmp:=aa[m];
aa[m]:=aa[2*j+k-m-1];
aa[2*j+k-m-l]:=tmp;
end;
{$R+} end;
begin clrscr;
writeln(' : ') ;
for i:=l to
N do
begin a[i]:=i;
write(a[i]:3);
end;
writeln;
invertl(a,1,K);
writeln(' :');
for i:=l to N do
write(a[i]:3);
writeln;
invertl(a,K+l,N-K);
writeln(' :');
for i:=l to N do write(a[i]:3);
writeln; invertl(a,l,N);
writeln(' :');
for i:=l to N do
write(a[i]:3);
readln;
end.
4.05.
() m x n
(w) (row ,
col , ). ,
.
1 ()
window,
.
, . . ,
, ,
. , ,
(. gettextinf ), QBasic
. . ,
.
LOCATE (QBasic) gotoxy (, ).
2 (QBasic)
, ,
PRINT USING.
, ,
w "I".
3 ()
, , "%wd".
w,
w+48, 48 "0".
4 ()
write ,
( w).
4_05.bas
DECLARE SUB
PRINTA (ROW%, COL%, W%, C% (-) , N%, M%)
DEFINT A-Z
CLS
DIM A(2, 3)
, B(3, 3)
FOR J = 0
2: FOR = 0 TO 3
A(J, K) = J + * NEXT : NEXT J PRINTA 5, 5, 3, A() , 3, 4
FOR J = 0 TO
3: FOR = 0 3
B(J, K) = J * 10 + * 25 NEXT K: NEXT J PRINTA 5, 40, 5, B() , 4, 4
END
SUB PRINTA (ROW%,
COL%, W%, C%(), N%, M%) DEFINT A-Z
F$ = LEFT$("##########", W)
FOR J = 0 TO N - 1: FOR = 0 TO M - 1
LOCATE ROW + J, COL + K*W
PRINT USING F$; C(J, K) NEXT K: NEXT J
END SUB
4_05.
#include <stdio.h>
#include <conio.h>
void printa(int
row,int col,int w,int *c,int n,int m) ;
void main(void)
{
int a[3] [4]
= {{!,2,3,4),{10,20,30,40},{100,200, 300, 400}};
int b[4][4]={{l,2,3,4},{5,6,7,8},{9,10,ll,12},{13,14,15,16}};
clrscr();
printa (5,5,4,
(int *)a,3,4);
printa(5,40,5,(int
*)b,4,4);
getch(); }
void printa(int
row,int col,int w,int *c,int n,int m) {
int j,k;
char f[4]="%0d";
f[1] += w;
for(j=0; j<n;
j++)
for(k=0; k<m;
k++)
{
gotoxy(col+k*w,row+j);
printf(f,c[k+j*m]);
}
}
4_05.pas
program mat_print;
uses crt;
const
a:array [1..3,1..4]
of integer = ((1,2,3,4), (10,20,30,40), (100,200,300,400));
b:array [1..4,1..4]
of integer = ((1,2,3,4), (5,6,7,8), (9,10,11,12), (13,14,15,16));
procedure printa(row,col,w:integer,-var ;n,m:integer);
var
j,k:integer;
d:array [0..MaxInt-l] of integer absolute c;
begin
for j:=0 to n-1 do
for k:=0 to m-1 do
begin
gotoxy(col+k*w,row+j); write(d[k+m*j]:w);
end;
end;
begin clrscr;
printa(5, 5, 4, a, 3, 4);
printa(5, 40, 5, b, 4, 4);
readln;
end.
4.06.
, .
, , . .
, .
.
, , , ,
. ,
,
. ""
,
. .
,
"". , . .
()
90
() .
8x8, a[i,j] 8
:
1. a[i-2,j-1]
5. a[i+l,j-2]
2. a[i-2,j+l]
6. a[i+l,j+2]
3. a[i-l,j-2]
7. a[i+2,j-1]
4. a [i-1,j+2]
8. a[i + 2,j + 1]
- -
.
, 2, .
, a[i1,j1] a[i2,j2],
. - ,
1, , .
a[i1,j1] 0 ,
. 1. ,
, ,
2. , ,
, . . .
, ,
3.
,
, .
,
[12, j2]. , ,
, a
[i1, j1] .
,
b[i2,j2]. n- ,
a[i1, j1] [i2, j2],
. , ,
a[i,j] + b[i,j]
= n.
,
a [i, j ]
.
, , , .
, ,
,
.
1 ()
,
k-ro . newlevel ,
, (k+l)-ro , ,
.
4_06.bas
DIM A(8,8)
FOR I=0 7
: FOR J=0 TO 7
A(I,J)=-l NEXT
J: NEXT I
CLS : INPUT
" :",I,J A(I,J)=0 : K=0 M:NEWLEVEL
IF XOD = 1 THEN
GOTO M
FOR I=0 TO 7
: FOR J=0 TO 7
PRINT A(I,J)
NEXT J: PRINT : NEXT I END
SUB NEWLEVEL
XOD=0
FOR I=0 TO 7 : FOR J=0 TO 7
IF A(I,J)=K THEN
TRY(I-2,J-l): TRY(I-2,J+1)
TRY(I-1,J-2): TRY(I-l,J+2)
TRY(I+1,J-2): TRY(I+1,J+2)
TRY(I+2,J-l): TRY(I+2,J+1)
END IF NEXT J:
NEXT I
K=K+1
END SUB
SUB TRY(P,Q)
IF P>=0 AND
P<8 AND Q>=0 AND Q<8 AND A(P,Q)<0 THEN A(P,Q)=K+1 : XOD=1
END IF
END SUB
4_06.
#include <stdio.h>
#include <conio.h>
char newlevel(void);
void try(int p,int q);
char a[8][8],i,j,k=0,xod;
void main(void)
{
clrscr();
for(i=0; i<8; i++)
for(j=0; j<8;
j++)
a[i][j]=-l;
puts(" :");
scanf("%d %d",&i,&j);
a[i][j]=0; m:newlevel();
if(xod==l) goto m;
for(i=0; i<8;
i++) {
for(j=0; j<8; j++)
printf("%3d",a[i] [j]) = 1;
printf("\n");
}
getch(); )
char newlevel(void)
{
char di[8]={-2,-2,-l,-l,
1,1, 2,2};
char dj[8]={-l,
l,-2, 2,-2,2,-1,1};
char f;
xod=0;
for(i=0; i<8;
i++)
for(j=0; j<8; j++)
if (a[i] [j]==k)
for(f=0; f<8; f++)
try(i+di[f],j+dj[f]);
k++;
return xod;
}
void tryfint
p,int q) {
if(p>=0 &&
p<8 && q>=0 && q<8 && a[p][q]<0)
{ a[p][q]=k+l;
xod=l;} }
4_06.pas
program horst;
{=======================================
.
0 7. , , 0. , , 1, - 2 . .
=====================================}
uses Crt;
label m;
var
i,j,k,xod:byte;
a:array [0..7,0..7]
of shortint;
procedure try(p,q:integer);
{=================================
k+1 (p,q). [,q]
, xod - 1. xod
0.
==================================}
begin
if (p>=0)
and (p<8) and (q>=0) and (q<8) and (a[p,q]<0) then
begin a[p,q]:=k+l;
xod:=l; end; end;
procedure newlevel;
{================================
k+1 , k
================================}
const
{ i,j 8 }
di:array [0..7] of integer = (-2,-2,-1,-1, 1,1, 2,2);
dj:array [0..7]
of integer = (-1, l,-2, 2,-2,2,-1,1); var
f:byte; begin
xod:=0; { }
{ , k- )
for i:=0 to 7 do
for j:=0 to 7
do
if a[i,j]=k
then
{ ,
8 }
for f:=0 to 7 do try(i+di[f],j+dj[f]);
k:=k+l;
{ }
end;
begin
k:=0; {
} clrscr;
for i:=0 to 7 do
for j:=0 to 7
do
a[i,j]:=-1; { }
write(' : '};
readln(i,j);
a[i,j]:=0; ( )
m: newlevel;
{
}
if xod=l then
goto m; { } {
}
for i:=0 to
7 do
begin
for j:=0 to
7 do
write (a[i,j]:3);
writeln;
end;
readln;
end.
4.07.
, . ,
, 15 000 .
,
. . randomize.
,
.
1 ()
( =
20) ( = 15 000, QBasic = 5000).
, .
.
, .
2 ()
,
. .
4_07.bas
DECLARE SUB
BUBBLE(X%(),N%)
DECLARE SUB
INSERT(X%(),N%)
DECLARE SUB
SELECT1(X%(},N%)
DECLARE SUB
SHELLSORT(X%(),N%)
DECLARE SUB
HOARE(X%(),N%)
DECLARE SUB
QUICK(X%(),LEFT%,RIGHT%)
DEFINT A-Z
CLS
CONST N=5000
DIM A(N)
FOR J=0 TO N
A(J)=INT(N*RND) 'PRINT USING "#### "; A(J); NEXT J
PRINT T1#=TIMER
BUBBLE A() , N ' INSERT A (),N 'SELECT1 A(),N 'SHELLSORT A(),N 'HOARE A{},N
T2#=TIMER
PRINT T2#-T1#;
""
'FOR J=0 TO
N: PRINT USING "#### ";A(J); : NEXT J END
REM
4_07.
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#define MAX 15000
void bubble(int
*x, int n) ;
void select(int
*x, int n) ;
void insert(int
*x, int n);
void shell(int
*x, int n);
void hoare(int
*x, int n);
void quick(int
*x, int left, int right);
main( ) {
int num[MAX];
int i ;
struct time
w;
clrscr();
//cout <<
" " << endl;
for(i=0; i <
MAX; i++) // {
num[i]=random(MAX);
// cout
<< num[i] << " ";}
// cout << endl; // getch();
// cout <<
" " « endl;
gettime (&w);
cout <<
(unsigned int)w.ti_sec <<".";
cout <<
(unsigned int)w.ti_hund << endl;
bubble(num,MAX);
//select(num,MAX);
//insert(num,MAX);
//shell(num,MAX);
//hoare(num,MAX);
gettime(&w);
cout <<
(unsigned int)w.ti_sec <<".";
cout << (unsigned int)w.ti_hund << endl;
//cout <<
"";
getch();
//for(i=0; i<MAX; i++)
//cout << num[i] << " ";
//getch();
return 0; }
/*====
========*/
4_07.pas
program all_sort;
uses crt,WinDos; const
MAX=15000; type
xarr=array [0..MAX-1]
of integer; var
x:xarr;
i:integer;
hour,minute,second,sec100:word;
{=====
======}
begin
clrscr;
for i:=0 to
MAX-1 do
begin
x[i]:=random(MAX);
{write(x[i],' ');}
end;
writeln;
write('
- ');
gettime(hour,minute,second,sec100);
write(minute:2,'
',second:2,'.',sec100:2, ' ');
writeln;
{ }
{ bubble(x,MAX);}
{ select(x,MAX);}
{ insert(x,MAX);}
shell(x,MAX);
{ hoare(x,MAX);}
gettime(hour,minute,second,sec100};
write('
- ');
write(minute:2,'
',second:2,'.',seclOO:2,' ');
writeln;
{ for i:=0 to
MAX-1 do write(x[i],' ');}
readln; end.
4.08.
"",
. ,
000000 999999,
.
1 ()
" " , ,
.
. ,
.
1000 , ,
0, 1, 2.....27. ,
000, 000000. ,
1, 001, 010 100. ,
9 (
). , s [0], s [1].....S
[27] ,
S, N :
N = S[0] * S[0]
+ S[l] * S[l] + ... + S[27] * S[27].
4_08.bas
DIM S(28)
FOR Al=0 TO
9: FOR A2=0 TO 9: FOR A3=0 TO 9
S(A1+A2+A3)=S(A1+A2+A3)+1 NEXT A3: NEXT A2: NEXT A1
FOR K=0 TO 27: N=N+S(K)*S(K) : NEXT
PRINT " = ";N
END
4_08.
#include <stdio.h>
#include <conio.h>
main()
{
int s[28],al,a2,a3,k;
long N;
clrscr();
for(k=0; k<28; k++)
s[k]=0;
for(al=0; al<10; al++)
for(a2=0; a2<10; a2++)
for(a3=0; <10;
3++)
s[al+a2+a3]++;
for(k=0,N=0;
k<28; k++)
N += s[k] *s
[k] ;
printf("\n = %ld",N);
getch(); }
4_08.pas
program lucky_ticket;
var
al,a2,a3,k:integer;
N:longint;
s:array [0..27] of integer;
begin
for al:=0 to 9 do
for a2:=0 to 9 do
for a3:=0 to
9 do
inc(s[al+a2+a3]);
for k:=0 to 27
N:=N+s [k] *s["k]
;
writeln(' = ',N);
readln;
end.
4.09.
,
.
,
.
1 ()
( ) .
.
, sort ,
.
2 ()
. ,
. ,
k0 = 0, k0 = 1.
, 1-
. , i-
.
, k0.
3 ()
.
[0,32767], 32 768
(4096 ). ,
.
a[i]
, 0, 1
. ,
.
4 ()
(b)
,
. calloc,
. byte bit
,
a [i].
4_09a.bas
DECLARE SUB
SORT (A() AS INTEGER, N%)
DECLARE FUNCTION
DIFFERENCE% (A() AS INTEGER, N%)
DEFINT A-Z
DIM A(5)
DATA 0,0,0,0,0
DATA 1,1,1,1,1
DATA 0,1,1,1,1
DATA 0,0,1,1,2
DATA 0,1,2,3,4
DATA 1,2,3,4,5
CLS
FOR k=1 TO 5
FOR I=0 TO 4:
READ A(I): NEXT I
PRINT "
";k;" = ";
PRINT DIFFERENCE(A(),
5} NEXT k END
FUNCTION DIFFERENCE
(A() AS INTEGER, N%)
SORT A(),N%
M=l
FOR I=0 TO N%-2
IF A(I)<>A(I+1)
THEN M=M+1
NEXT I
DIFFERENCE=M
END FUNCTION
SUB SORT(A()
AS INTEGER,N%)
REM
END SUB
4_09b.bas
DECLARE SUB
SORT (A() AS INTEGER, N%)
DECLARE FUNCTION
DIFFERENCE% (A() AS INTEGER, N%)
DEFINT A-Z
DIM A0(5),A1{5),A2(5),A3(5},A4(5),A5(5)
DATA 0,0,0,0,0
FOR I=0 TO 4:
READ 0(I): NEXT I
DATA 1,1,1,1,1
FOR I=0 TO 4:
READ A1(I): NEXT I
DATA 0, 1,1,
1, 1
FOR I=0 TO 4:
READ A2(I): NEXT I
DATA 0,0, 1,
1,2
FOR I=0 TO 4:
READ A3(I): NEXT I
DATA 0,1,2,3,4
FOR I=0 TO 4:
READ A4(I): NEXT I
DATA 1,2,3,4,5
FOR I=0 TO 4:
READ A5(I): NEXT I
PRINT "
0 = ";
PRINT DIFFERENCE(0(),5)
PRINT "
1 = ";
PRINT DIFFERENCE(A1(),5)
PRINT "
2 = ";
PRINT DIFFERENCE(A2(),5)
PRINT "
A3 = ";
PRINT DIFFERENCE(A3()
,5)
PRINT "
4 = ";
PRINT DIFFERENCE(A4(),5)
PRINT "
5 = ";
PRINT DIFFERENCE(A5(),
5)
END
FUNCTION DIFFERENCE
( AS INTEGER-,N%) DEFINT A-Z
FOR I=0 TO N%-1
IF A(I)=0 THEN
K0=l: EXIT FOR NEXT I
FOR I=0 TO N%-1
IF A(I)<>0 THEN
FOR J=I+1 TO
N%-1
IF A(I)=A(J) THEN A(I)=0: EXIT FOR NEXT J END IF NEXT I
FOR I=0 TO N%-1
IF A(I)<>0
THEN M=M+1 NEXT I
DIFFERENCE=M+K0
END FUNCTION
4_09.
#include <stdio.h>
#include <conio.h>
void sort(int
*a,int n);
int difference(int
*a,int n);
main() {
int a0[5]={0,0,0,0,0};
int a1[5]={l,l,l,l,l};
int a2[5]={0,l,l,l,l);
int a3[5]={0,0,l,l,2};
int a4[5]={0,l,2,3,4};
int a5[5]={l,2,3,4,5};
printf("\n
");
printf("%d",difference(a0,5));
getch(); }
void sort(int
*a,int n) {
/*
*/
return; }
int difference(int
*a, int n) {
int i,m;
sort(a,n);
for(i=0,m=l; i<n-l; i++)
if(a[i] != a[i+l])
m++;
return m; }
4_09b.c
#include <stdio.h>
#include <conio.h>
int difference(int
*a,int n);
main() {
int a0[5]={0,0,0,0,0};
int al[5] =
{l,l,l,l,1};
int 2[5]={0,1,1,1,1};
int a3[5]={0,0,l,l,2};
int a4[5]={0,l,2,3,4};
int a5[5]={l,2,3,4,5};
printf("\n ");
printf("%d",difference(a0,5));
getch (); }
int difference(int
*a,int n) {
int i,j,k0,m;
for(i=k0=0;
i<n; i++)
if(a[i] == 0) { k0=1; break; }
for(i=0; i<n-l;
i++)
{ if(a[i]==0) continue;
for(j=i+l; j<n;
j++)
if(a[i]==a[j]) { a[i]=0; break; } }
for(i=m=0; i<n;
i++)
if(a[i] !=0)m++;
return m+k0;
}
4_09.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
int difference(int
*a,int n);
main() {
int a0[5]={0,0,0,0,0};
int a1[5]={l,l,l,l,l};
int a2[5]={0,l,l,l,l};
int a3[5]={0,0,l,l,2);
int a4[5]={0,l,2,3,4);
int a5[5]={l,2,3,4,5};
printf("\n
");
printf("%d",difference(a5, 5)) ;
getch(); }
int difference(int
*a, int n) {
char *b;
char mask[8]={128,64,32,16,8,4,2,1};
int bit,byte,i,m;
b=calloc(4096,1);
for(i=m=0; i<n;
i++) {
byte = a[i]/8; bit = a[i]%8;
if ((b [byte]
& mask [bit] )=0)
{ m++; b[byte]
| = mask[bit]; } }
free(b); return
m; }
4_09a.pas
program dif;
const
a0:array [0..4] of integer=(0,0,0,0, 0)
al:array [0..4] of integer=(l,1,1,1,1)
a2:array [0..4] of integer=(0,1,1,1,1)
a3:array [0..4] of integer=(0,0,1,1, 2)
a4:array [0..4] of integer=(0,1,2,3,4)
a5:array [0..4]
of integer=(1,2, 3, 4, 5)
procedure sort(var a:array of integer);
begin end;
function difference(a:array of integer):integer;
var i,m: integer;
begin
sort(a);
m:=l;
for i:=0 to
High(a)-1 do
if a[i] <>
a[i+l] then inc(m);
difference:=m;
end;
begin
write('
');
write(difference(a5));
readln; end.
4_09b.pas
program difl;
const
a0:array [0..4J
of integer=(0,0,0,0,0)
a1:array [0..4]
of integer= (1,1,1,1,1)
a2:array [0..4]
of integer=(0,1,1,1,1)
a3:array [0..4]
of integer=(0,0,1,1,2)
a4:array [0..4]
of integer=(0,1,2,3,4)
a5:array [0..4]
of integer=(1,2,3,4,5)
function difference(a:array of integer):integer;
var
i,j,k0,m,n:integer;
begin
k0:=0; n:=High(a);
for i:=0 to
n do
if a[i]=0 then begin k0:=l;
break;
end;
for i:=0 to
n-1 do begin
if a[i]=0 then
continue; for j:=i+l to n do
if a[i]=a[j] then begin a[i]:=0;
break; end; end ;
m:=0; for i:=0
to n do
if a[i]<>0 then inc(m);
difference:= m+k0;
end;
begin
write('
');
write(difference(a5)};
readln;
end.
4.10. " "
.
1 ()
, , .
, [0,35] (
[0,51]). (,
, , , ) (, :
6, 7, 8, 9, 10, , , , ).
, -,
(, "6" , "104"
, "" ).
2 ()
. . (i, j) i- j-
. ,
10 000 , " " .
3 ()
,
, , i- "",
.
4 ()
,
( randomize).
4_10.bas
DECLARE SUB
MIXER(() AS INTEGER)
DEFINT A-Z
DIM A(36)
CLS
PRINT "
:"
FOR K=0 TO 35:
A(K)=K: PRINT USING "####";A(K); : NEXT
FOR J=0 TO 4
MIXER A()
PRINT "
:"
FOR K=0 TO 35:
PRINT USING "####";A(K); : NEXT NEXT J END
SUB MIXER(B()
AS INTEGER) DEFINT A-Z
RANDOMIZE INT(32767*RND)
FOR J=0 TO 10000
I=INT(35*RND+.5)
TMP=B(0)
B(0)=B(I)
B(I)=TMP
NEXT J
END SUB
4_10.
#include <stdlib.h>
#include <conio.h>
void mixer(char *b);
main()
{
char j,k, a[36];
clrscr();
printf("\n :\n");
for (k=0; k<36;
k++) {
a[k]=k;
printf("%4d",a[k]);
}
for(j=0; j<5;
j++) {
mixer(a);
printf("\n
:\n");
for(k=0; k<36;
k++)
printf("%4d",a[k]);
begin clrscr;
writeln(' :') ;
for k:=0 to
35 do begin
a[k]:=k; write(a[k]:4);
end;
writeln;
for j:=0 to
4 do begin
mixer(a);
writeln(' :');
for k:=0 to
35 do
write(a[k]:4);
writeln;
end;
readln;
end.
4.11.
:
1 ()
, 2 ,
, 0. ,
0.
.
, (
), , , ,
.
2 ()
:
3 ()
,
.
3 5,
[1,12]. (
) ( ) ,
.
4_11.bas
DECLARE SUB
USER()
DECLARE SUB
START{)
DECLARE SUB
COMPUTER()
DECLARE SUB
PRINTXOD(col%,msg$)
DEFINT A-Z
DIM SHARED N
DIM SHARED
AS INTEGER,S AS INTEGER,M AS INTEGER,Q AS INTEGER
N=5: Q=12
DIM SHARED A(N)
AS INTEGER,I AS INTEGER,J AS INTEGER, AS INTEGER
M=l
START m1:
USER
COMPUTER
GOTO ml END
SUB COMPUTER
S=0
FOR I=0 TO K-1:
S=S XOR A(I): NEXT I
IF s=o THEN
J=0: S=A(0)
FOR I=1 TO K-1
IF S<A(I)
THEN S=A(I): J=I
B=l
NEXT I ELSE
FOR J=0 TO K-1
B=A(J)-(A(J) XOR S)
IF B>=0 THEN EXIT FOR NEXT J END IF
A(J)=A(J)-B
PRINTXOD 4, " "
END SUB
SUB PRINTXOD(col%,msg$)
COLOR col%, 0 FOR I=0 TO K-1
LOCATE M+l,3*I+1:
PRINT USING "###";A(I)
NEXT I
M=M+1: IF M>23
THEN M=2: CLS: PRINTXOD col%, msg$'
S=0
FOR J=0 TO K-l: S=S+A(J): NEXT J
IF S<>0 THEN EXIT SUB LOCATE M,2: PRINT msg$ SLEEP STOP
END SUB
SUB START
CLS : RANDOMIZE
(VAL(RIGHTS(TIME$, 2)))
K=INT(RND*(N-3))+3:
'
FOR I=0 TO K-l
A(I)=INT(RND*Q)+1
NEXT I
LOCATE 1,2 :
PRINT " "
PRINTXOD 4,""
END SUB
SUB USER COLOR
2,0
LOCATE M, 20
PRINT "
( - ):"
2:
LOCATE M, 33:
PRINT ""
LOCATE M,33:
INPUT J
IF (J<1) OR (J>K) OR (A(J-1)=0) THEN GOTO M2
m3:
LOCATE M, 35:
PRINT "-"
LOCATE M, 37:
INPUT
IF (B<1)
OR (B>A(J-1J) THEN GOTO m3
A(J-1)=A(J-1)-B
SLEEP
PRINTXOD 2, " "
END SUB
4_11.c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void print_xod(int
color,char *msg);
void start(void);
void user(void);
void computer(void);
#define N 5
#define Q 12
int i,j,k,b,s,m=l;
int a[N];
main() {
start(); ml:
user () ;
computer() ;
goto ml; }
void start(void)
{
clrscr();
randomize();
k=random(N-3)+3;
/* */
for(i=0; i<k; i++)
a[i]=random(Q)+1;
print_xod(RED,"
"); }
void user(void)
{
textcolor(GREEN);
gotoxy(20,m-l); cprintf(" ( - ):");
m2:
gotoxy(33,m);
cprintf("
") ;
gotoxy(33,m);
scanf("%d",&j);
if((j<l) || (j>k) || (a[j-l]=0)) goto ra2;
m3:
gotoxy(35,m);
cprintf("- ") ;
gotoxy(37,m);
scanf("%d",&b);
if((b<l)
|| (b>a[j-l])) goto m3;
a[j-l]-=b;
print_xod(GREEN,"
");
return; }
void computer(void)
{
for(s=0, i=0; i<k; i++)
s ^= a[i];
if(s==0) {
for(i=l,j=0,s=a[0]; i<k; i++)
if(s<a[i])
{s=a[i]; j=i;} b=l; }
else
for(j=0; j<k; j++)
(b=a[j]-(a[j]"s);
if(b>=0)
break;}
a[j]-=b;
print_xod(RED,"
");
return; }
void print_xod(int
color,char *msg) (
textcolor(color);
for(i=0; i<k;
i++)
gotoxy(3*i+l,m) ;
cprintf("%3d",a[i]);}
m++;
if(m>23)
{
m=2;
clrscr();
print_xod( color, msg) ;
}
for(j=0,s=0; j<k; j++)
s+=a[j];
if(s!=0) return;
gotoxy(l,m);
cprintf("%s",msg);
getch();
exit(0); }
4_11.pas
program nim;
uses Crt; const
n=5; q=12; var
i,j,k,b,s,m:integer;
a:array [l..n] of byte; label ml;
procedure print_xod(color:integer);
begin textcolor(color);
for i:=l to
k do begin
gotoxy(3*i,m);
write(a[i]:2);
end;
m:=m+l;
if m > 23 then begin clrscr;
m:= 2;
print_xod(color);
end;
end;
procedure start;
begin
clrscr; randomize;
k:=random(n-3)+3;
{ }
m:=1;
if m>23 then begin clrscr;
m:=2;
print_xod(color);
end;
for i:=l to k do
a[i]:=random(q)+1;
print_xod(RED);
end;
procedure user;
label m2,m3;
begin
textcolor (GREEN)
;
gotoxy(20,m-1);
write(' ( - ):');
m2:
gotoxy(33,m);
write(' ');
gotoxy(33,m)
; read(j);
if(j<l) or (j>k) or (a[j]=0) then goto m2;
m3:
gotoxy(35,m);
write('- ');
gotoxy(37,m);
read(b);
if(b<l) or (b>a[j]) then goto m3;
a[j]:=a[j]-b;
print_xod(GREEN);
end;
procedure computer;
begin s:=0;
for i:=l to k do s:=s xor a[i];
if s=0 then begin s:=a[l];
j:=l;
for i:=2 to k
do
if s<a[i]
then begin
s:=a[i];
j:=i;
end;
b:=l;
end
else
for j:=l to k do begin b:=a[j]-(a[j] xor s) ;
if b>=0 then
break; end;
a[j]:=a[j]-b;
print_xod(RED);
end;
begin
start;
ml : user;
s:=0;
for j:=l to k do s:=s+a[j];
if s=0 then
begin
gotoxy(1,m);
write(' ');
readln; readln;
exit; end;
computer; s:=0;
for j:=l to k do s:=s+a[j];
if s<>0 then goto ml;
gotoxy(l,m);
write(' ');
readln;
readln;
end.
4.12. "-"
- 3x3. :
, .
, . ,
( , ,
). ,
, .
, "" ,
, , .
.
:
a)
, (
), ;
b) )
, ,
;
c)
), ),
.
:
a)
, , ;
b)
, ;
c)
), b), .
1 ()
"-"
, ,
. .
. ,
.
,
5 ( ) 3 ( )
.
, .
1 9
:
() () :
,
.
.
1 8 :
lines,
.
.
, .
,
a[j]. show(j, k),
k [ j ]
( k +2 -2, ,
). ,
. . .
. input (
QBasicUSER, . . INPUT ).
. <Enter>.
.
(
beep 7).
( ,
). show (5, 2).
.
j (j = l, 2, 3, 4,
, 7, 8 9),
b [ j ].
. step345,
:
.
a[j], , .
4,
. , .
xod(k1 = 2, k2 = 2).
, [ j ] 2*k1, , ,
"" k2. xod
"", .
. ,
, -4, ,
. ,
. xod,
k1 = -2 k2 = 2. ,
xod "".
. ,
lines.
, .
.
.
[ j ].
, , ,
(+2), , (-2).
( ) 18. ,
, .
2 (QBasic)
LOCATE. -, , gotoxy,
( , ). -,
, ,
. , ,
.
3 (, )
(window). (0, 0),
.
4_12.bas
DECLARE SUB
STEP345()
DECLARE SUB
RESULT(s$)
DECLARE SUB
CURIND(cur%,porog%,dcur%,dind%)
DECLARE SUB
SHOW(k%,c%)
DECLARE SUB
USER()
lines,
.
.
, .
,
a[j]. show(j, k),
k [ j ]
( k +2 -2, ,
). ,
. . .
. input (
QBasic USER, . . INPUT ).
. <Enter>.
.
(
beep 7).
( ,
). show(5, 2).
.
j (j =l, 2, 3, 4,
, 7, 8 9),
b [ j ].
. step345,
:
.
a[j], , .
4,
. , .
xod(k1 = 2, k2 = 2).
, [ j ] 2*k1, , ,
"" k2. xod
"", .
.
, , -4, ,
. ,
.
xod, kl = -2 k2 = 2.
, xod "".
. ,
lines.
, .
.
.
[ j ].
, , ,
(+2), , (-2).
( ) 18. ,
., .
2 (QBasic)
LOCATE. -, , gotoxy,
( , ). -,
, ,
. , ,
.
3 (, )
(window). (, ),
.
4_12.bas
DECLARE SUB
STEP3450
DECLARE SUB
RESULT(s$)
DECLARE SUB
CURIND(cur%,porog%,dcur%,dind%)
DECLARE SUB
SHOW(k%,c%)
DECLARE SUB
USER()
DECLARE FUNCTION
XOD!(k%,kl%)
DEFINT A-Z
DIM SHARED POS1(1
TO 18) AS INTEGER,lines(0 TO 23) AS INTEGER
DIM SHARED A(l
TO 9) AS INTEGER,b(l TO 9) AS INTEGER
DIM SHARED x0
AS INTEGER,0 AS INTEGER
DIM SHARED Ind
AS INTEGER,CurX AS INTEGER,CurY AS INTEGER
FOR k=l TO 9: A(k)=0: NEXT k
DATA 3,1,1,1,5,3,1,7,3
FOR k=l TO 9:
READ b(k): NEXT k x0=l: y0=l
DATA 3,2, 7, 2, 11, 2, 3,4,
7, 4, 11, 4,3, 6, 7, 6, 11, 6
FOR k=l TO 18:
READ POSl(k): NEXT k
DATA 1,2,3,4,5,6,7,8,9,1,4,7,
2,5,8,3,6,9,1,5,9,3,5,7
FOR k=0 TO 23:
READ lines(k): NEXT k
CLS
SHOW 5,2 '
USER '
1-
FOR J=l TO 9
'
IF A(J)=-2 THEN
SHOW b(J),2: EXIT FOR NEXT J
m2: USER '
STEP345 '
GOTO m2 END
DEFSNG A-Z
SUB CURIND(cur%,porog%,dcur%,dind%)
IF curoporog
THEN cur=cur+dcur: Ind=Ind+dind END SUB
DEFSNG A-Z SUB
RESULT(s$)
LOCATE 1,40:
PRINT s$: END END SUB
DEFINT A-Z
SUB SHOW(k%,C%)
DEFINT A-Z CLS
LOCATE x0,y0:
PRINT "+-------------------+"
LOCATE x0+l,y0:
PRINT "| | | |"
LOCATE x0+2,
y0: PRINT " |-----|-----|-----| "
LOCATE x0+3,y0:
PRINT "| | | |"
LOCATE x0+4,y0:
PRINT "|-----|-----|-----|"
LOCATE x0+5,y0:
PRINT "| | | |"
LOCATE x0+6,y0:
PRINT "+-----+-----+-----+"
A(k%)=c% FOR
J=l TO 9
LOCATE y0-l+POSl(J*2),x0-1+POSl(J*2-l)
IF A(J)=2 THEN
PRINT "X"
IF A(J)=-2 THEN
PRINT "0" NEXT J
CurX=xO+6: CurY=y0+3:
Ind=5 LOCATE CurY,CurX,l END SUB
SUB STEPS45
DEFINT A-Z
IF XOD(2,2)=1
THEN RESULT " ": END
IF XOD(-2,2)=1
THEN EXIT SUB
FOR J=l TO 9
IF A(J)=0 THEN
SHOW J,2: EXIT FOR
NEXT J END SUB
DEFSNG A-Z
SUB USER
DEFINT A-Z
Left=75: Right=77: Up=72: Down=80: Enter=13 k=0:
FOR J=l TO 9:
k=k+ABS(A(J)): NEXT J IF k=18 THEN RESULT " ": END
m: ch$=INKEY$:
IF LEN(ch$)=0 THEN GOTO m
SELECT CASE ASC(RIGHTS(ch$,1))
CASE Left: CURIND CurX,3,-4,-l
CASE Right: CURIND
CurX,ll,4/l
CASE Up: CURIND
CurY,2,-2,-3
CASE Down: CURIND
CurY,6,2,3
CASE Enter:
IF A(Ind)=0 THEN SHOW Ind,-2:
EXIT SUB
CASE ELSE: BEEP
END SELECT
LOCATE y0-l+CurY,x0-l+CurX,l
USER END SUB
DEFINT A-Z FUNCTION
XOD(k%,kl%)
DIM J AS INTEGER,m
AS INTEGER,p AS INTEGER XOD=0
FOR J=0 TO 7
m=J*3
IF A(lines(m))+A(lines(m+1))+A(lines(m+2))=2*k% THEN XOD=1
FOR p=m TO m+2
IF A(lines(p))=0 THEN SHOW lines(p),kl% EXIT FUNCTION END IF NEXT p
END IF NEXT J
END FUNCTION
4_12.
#include <stdio.h>
#include <conio.h>
void input(void);
void cur_ind(int
*cur,int porog,int dcur,int dind);
void step345(void);
int xod(int
k, int kl);
void showfint
k, int c);
void result(char
*s);
int a[9]={0,0,0,0,0,0,0,0,0},
b[9]={3,l,l,l,5,3,l,7,3},
0=1, 0=1,
j, CurX, CurY, Ind; main()
{
clrscr();
window(x0,y0,x0+13,y0+7);
show(5,2); /*
*/
input(); /*
1- */
for(j=0; j<9;
j++) /* */
if (a[j]==-2) { show(b[j],2); break; }
m2:
input(); . /*
*/
step345(); /*
*/
goto m2;
}
/*--------------------------------*/
void result(char
*s) {
window(40,l,60,2);
puts(s);
getch () ;
exit(0);
}
/*-------------------------------*/
void show(int
k,int c) {
char pos[18]=(3,2,7,2,11,2,3,4,7,4,11,4,3,6,7,6,11,6};
char j;
clrscr ();
printf("+-----------+\n");
printf("|
| | |\n");
printf ( "|----|----|----|
\n") ;
printf("|
| | |\n");
printf ("
|----|----|----| \n"} ;
printf("|
| | |\n");
printf ("+----------------+")
;
a[k-l]=c;
for(j=0; j<9;
j++) {
gotoxy(x0-l+pos[j*2],y0-l+pos[j*2+l]);
if(a[j]==+2)
printf("X");
if(a[j]==-2)
printf("0"); }
CurX=x0+6; CurY=y0+3;
Ind=5; gotoxy(CurX,CurY);
}
/*-------------------------------*/
int xod(int
k, int k1) {
char line[24]={1,2,3,4,5,6,7,8,9,1,
4,7,2,5,8,3,6,9,1,5,9,3,5,7},
j,m,p;
for(j=0; j<8;
j++)
{
m=j*3;
if(a[line[m]-1)+a[line[m+1]-1]+a[line[m+2]-1]==2*k)
{
printf ("\nm=%d",m) ; for(p=m; p<m+3; p++)
if(a[line[p]-l]==0)
{
show(line[p],kl);
return 1; } } } return 0;
}
/*-------------------------------------*
/
void step345(void)
{
if(xod( 2,2)) result(" ");
if(xod(-2,2)) return;
for(j=0; j<9; j++)
if(a[j]==0)
{
show(j+l,2);
break;
} }
/*----------------------------*/
void cur_ind(int
*cur, int porog, int dcur, int dind) {
if( *cur !=
porog) {
*cur += dcur;
Ind += dind;
}
}
/*----------------------------*/
void input(void)
{
int ch, j, k;
for(k=0, j=0;
j<9; j++) k+=abs(a[j]);
if(k==18) result("
");
ch=getch();
if(ch==0) ch=getch();
switch (ch)
{
case 75: cur_ind(&CurX, 3,-4,-l);break; /*Left*/
case 77: cur_ind(&CurX,11, 4, 1); break; /*Right*/
case 72: cur_ind(&CurY, 2,-2,-3); break; /*Up*/
case 80: cur_ind(&CurY, 6, 2, 3); break; /*Down*/ case 13:
printf("\nInd=%d",Ind);
/*Enter*/
if(a[Ind-l]==0)
{ show(Ind,-2); return;}
else
printf("%c",0x7);
break;
default: printf("%c",0x7);
}
gotoxy(x0-l+CurX,y0-l+CurY);
input(); }
4_12.pas
program krestiki;
uses Crt;
label m2;
const
a-.array [1..9]
of integer = (0,0,0,0,0,0,0,0,0);
b:array [1..9]
of byte = (3,1,1,1,5,3,1,7,3);
x0=5; y0=5;
var
j, CurX, CurY, Ind : word; procedure result(s:string);
begin
window(40,1,60,2);
write(s);
readln; halt;
end;
procedure show(k,:integer) ;
const
pos:array [1..18] of byte = (3,2,7,2,11,2,3,4,7,4,11,4,3,6,7,6,11,6);
var
j:byte;
x,:word;
begin
clrscr;
writeln (' +----+----+----+')
writeln('| |
| |')
writeln (' |----|----|----|
')
writeln('| |
| |')
writeln(' |----|-----|-----|')
writeln('| | | |')
write (' +----+----+----+')
a[k]:=c;
for j:=1 to
9 do begin
x:=pos[(j-l)*2+l]; y:=pos[(j-1)*2+2];
gotoxy(x,y);
if a[j]=+2 then write('X');
if a[j]=-2 then write('0');
end;
CurX:=7; CurY:=4;
Ind:=5;
gotoxy(CurX,CurY);
end;
function xod(k,kl:integer):boolean;
const
line:array [0..23]
of byte=
(1, 2, 3,4, 5, , 7, 8, 9,1,4, 7, 2, 5, 8, 3,6, 9,1,5, 9, 3,5,7) ;
var
j,m,p:byte;
begin
xod:=false;
for j:=0 to 7 do begin m:=j*3;
if a[line[m]]+a[line[m+1]]+a[line[m+2]]=2*k
then begin
xod:=true;
for p:=m to m+2 do
if a[line[p]]=0 then
begin
show(line[p],kl);
exit;
end;
end;
end;
end;
procedure step345;
begin
if xod( 2,2) then result(' ');
if xod(-2,2) then exit;
for j :=1 to
9 do if a[j]=0 then begin
show(j,2);
break;
end;
end;
procedure input;
const
Left=#75;
Right=#77;
Up=#72;
Down=#80;
Enter=#13; var
ch:char;
j,k:byte;
procedure cur_ind(var cur:word;porog,dcur,dind:integer) ;
begin
if cur <>
porog then begin
cur:=cur+dcur;
Ind:=Ind+dind;
end;
end; begin
k:=0;
for j:=l to 9 do
k:=k+abs(a[j]);
if k=18 then result(' ');
ch:=readkey;
if ch=#0 then ch:=readkey;
case ch of
Left: cur_ind(CurX, 3,-4,-l);
Right: cur_ind(CurX,11, 4, 1);
Up: cur_ind(CurY,
2,-2,-3);
Down: cur_ind(CurY, 6, 2, 3);
Enter: if a[Ind]=0
then begin
show(Ind,-2);
exit;
end
else write(tt7);
else write(#7);
end;
gotoxy(CurX,Cury);
input; end;
begin
clrscr;
window(x0,y0,x0+13,y0+7);
show(5,2);
{
}
input; {
1- }
for j:=1 to
9 do { }
if a[j]=-2 then
begin show(b[j],2);
break;
end;
m2: input; {
}
step345; {
}
goto m2;
end.
4.13.
,
, .
1 ()
,
m+n .
.
, .
, .
4_13.bas
DECLARE SUB
MERGE (A%(),NA%,B%(),NB%,C%())
DEFINT A-Z
CLS
NA=3:
DIM A(NA)
DATA 0,2,4
FOR K=0 TO NA-1:
READ A(K): PRINT A(K); : NEXT K: PRINT
NB=4
DIM B(NB)
DATA 1,3,5,7
FOR K=0 TO NB-1:
READ B(K): PRINT B(K); : NEXT K: PRINT
DIM C(NA+NB)
MERGE A(),NA,B(),NB,C()
FOR K=0 TO NA+NB-1:
PRINT C(K); : NEXT
END
SUB MERGE(A(),NA,(),NB,())
JA=0: JB=0
FOR JC=0 TO
NA+NB-1
IF JA=NA THEN
GOTO MB
IF JB=NB THEN
GOTO MA
IF A(JA)<B(JB)
THEN GOTO MA MB: C(JC)=B(JB): JB=JB+1: GOTO M1 MA: C(JC)=A(JA): JA=JA+1:
Ml:
NEXT JC
END SUB
4_13.
#include <stdio.h>
#include <conio.h>
void merge(int
*a,int ka,int *b,int kb,int *c);
main() {
#define na 3
#define nb 4
int j,a[na]={0,2,4},b[nb]={l,3,5,7},c[na+nb];
clrscr();
for(j=0; j<na; j++)
printf("%4d",a[j]);
printf("\n");
for(j=0; j<nb; j++)
printf("%4d",b[j]);
printf("\n");
merge(a,na,b,nb,c);
for(j=0; j<na+nb; j++)
printf("%4d",[j]);
getch(); }
void merge(int
*a,int ka,int *b,int kb,int *c) {
int ja=0,jb=0,jc;
for(jc=0; jc<ka+kb;
jc++) {
if (ja==ka) goto mb;
if (jb==kb) goto ma;
if (a[ja]<b[jb])goto ma;
mb:
c[jc]=b[jb];
jb++;
continue;
ma:
c[jc]=a[ja];
ja++;
} }
4_13.pas
program merge2;
uses Crt;
const
na=3 ;
nb=2 ;
a:array [0..na]
of integer = (0,2,4,6);
b:array [0..nb] of integer = (1,3,5);
var
c:array [0..na+nb+l]
of integer;
j:integer;
procedure merge(a,b:array of integer;var c:array of integer);
var
ja,jb,jc,na,nb,nc:integer;
label ma, mb;
begin
na:=High(a); ja:=0;
nb:=High(b);
jb:=0;
nc:=High(c);
if nc < na+nb+1
then begin
writeln(' ');
exit;
end;
for jc:=0 to
na+nb+1 do begin
if ja > na then goto mb;
if jb > nb then goto ma;
if a[ja] < b[jb] then goto ma;
mb:
[jc]:=b[jb]; inc(jb);
continue;
ma:
[j ]:=a[j a];
inc(j a};
end;
end;
begin clrscr;
for j:=0 to na do
write(a[j]:4);
writeln;
for j:=0 to nb do
write(b[j]:4) ;
writeln; merge(a,b,c);
for j:=0 to na+nb+1 do
write(c[j]:4) ;
readln; end.
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |