. - 3GL   - 4GL   5GL  

,

2.01.

20 . ( ), . - , , .

1

, . 7 ( 32 768 6 ). , , , 3 (2 [1,20] , ). , 10 , . , , .

2

- ? QBasic INKEY$. - , $, $ :

$=""

10 : A$=INKEY$ ; IF $="" THEN GOTO M10

- . , . . , <Enter>. , .

- getch.

, QBasic, KeyPressed:

while not KeyPressed;

INPUT $ QBasic read/readln . <Enter>.

2_01.bas

R , 20

DIM A(20)

F$="##:###### "

PRINT " 20 , "

FOR 1=0 19: INPUT A(I): S=S+A(I): NEXT I

FOR 1=0 TO 19: PRINT USING F$;I+1;A(I); : NEXT I

INPUT A$

FOR 1=0 TO 19: PRINT USING F$;I+1;A(I): NEXT I

PRINT "

PRINT USING " ######"; S

END

2_01.

/* , 20 */

#include <stdio.h>

#include <conio.h>

main() {

int j,s=0,a[20];

clrscr();

printf(" 20 , \n"

for(j=0; j<20; j++) { scanf("%d",&a[j]); s+=a[j]; }

for(j=0;j<20;

printf("%2d:%-6d ",j+l,a[j]>; for(j=0;j<20; j++)

printf("\n%2d:%6d ",j+l,a[j]) ; printf("\n -------\n%9d",s);

getch();

2_01.pas

program io_numbers;

{ , 20 }

uses Crt;

var

j, s:integer;

a:array [1..20] of integer; begin

s: =0 ;

clrscr;

writeln(' 20 , "!

for j:=1 to 20 do

begin readln(a[j]); s:=s+a[j]; end;

for j:=l to 20 do write(j:2,':',a[j]:6,' ');

writeln(' ';

readkey; clrscr;

for j:=l to 20 do writeln(j:2,':',a[j]:6,' ');

writeln(' --------');

writeln (s: 9).;

readln; end.

2.02.

2.01 , .

2.03. 2, 8, 16

. , .

1 (QBasic)

? $, . kaeaw

2 ()

Itoa, , . , . , , % %. (N = N « 1;), (N & 0x8000) .

3 ()

- , . . 4 , 32 . F. 10 .

2_03.bas

REM 2, 8 16

CLS

INPUT " : ",N&

$=$(N&)

PRINT " ";N&;"= ";

FOR k=l TO LEN(A$)

B$=MID$(A$,k,1) : '

SELECT CASE B$

CASE "": IF k=l THEN PRINT ""; ELSE PRINT "000";

CASE "1": IF k=l THEN PRINT "1"; ELSE PRINT "001";

CASE "2": IF k=l THEN PRINT "10"; ELSE PRINT "010";

CASE "3": IF k=l THEN PRINT "11"; ELSE PRINT "01l";

CASE "4": PRINT "100";

CASE "5": PRINT "101";

CASE "6": PRINT "111";

CASE "7": PRINT "111";

END SELECT

NEXT k

PRINT

PRINT " ";N&;"= ";OCT$(N&)

PRINT " ";N&;"=";HEX$(N&)

END

2_03a.bas

REM 2, 8 16
CLS

INPUT " : ",n&

$=$(&) : '

IF n&=0 THEN

PRINT " 0" STOP END IF PRINT " ";&;"= ";

B$=LEFT$($,1}: : ' SELECT CASE B$


SELECT CASE B$
CASE "0": PRINT "";
CASE "1": PRINT "1";
CASE "2": PRINT "10";
CASE "3": PRINT "11";
CASE "4": PRINT "100";
CASE "5": PRINT "101";
CASE "6": PRINT "111";
CASE "7": PRINT "111";
END SELECT
FOR K = 2 TO LEN(a$)
B$ = MID$(a$, K, 1)
SELECT CASE B$
CASE "0": PRINT "000";
CASE "1": PRINT "001";
CASE "2": PRINT "010";
CASE "3": PRINT "011";
CASE "4": PRINT "100";
CASE "5": PRINT "101";
CASE "6": PRINT "111";
CASE "7": PRINT "111";
END SELECT
NEXT K
PRINT

PRINT " ";n&;"= ";OCT$(n&)

PRINT " ";n&;"= ";HEX$(n&) END

END

2_03.

/* 2, 8 16 */

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

main()

{

long N;

char a[33];

clrscr();

printf(" : ") ;

scanf("%ld",&N);

if(N==0) {

printf("\n = 0") ;

exit(1);

}

ltoa(N,a,2);

/* */

printf("\n B %ld = %s",N,a);

ltoa(N,a,8);

/* */

printf("\n %ld = %s",N,a);

ltoa(N,a,16);

/* */

printf("\n %ld = %s",N,a);

getch();

}

2_03.pas

program _2_8__16;

{ 2, 8 16 }

uses crt; var

N1,N:longint;

a:array [0..31] of byte;

j,k:byte;

s:char; begin

clrscr;

write(' : ');

readln(N);

if N=0 then begin

writeln(' = 0');

exit;

end;

N1:=N;

for j:=0 to 31 do

a[j]:=0;

while Nl<>O do

begin

a[j]:=N1 mod 2; { }

dec(j);

N1:=N1 div 2;

end;

write(' ',N,'=');

for k:=j+l to 31

do write(a[k]:1);

writeln;

N1:=N;

for j:=0 to 10 do a[j]:=0;

while N1<>0 do begin

a[j]:=Nl mod 8; { )

dec(j);

N1:=N1 div 8;

end;

write (' ',N,'=');

for k:=j+l to 10

do write(a[K]:i);

writeln; N1:=N;

for j:=0 to 7 do a[j];=0;

while N1<>0 do begin

a[j]:=N1 mod 16;

dec(j);

N1:=N1 div 16;{ }

end; write(' ',N,'=');

for k:=j+l to 7 do begin

if a[k]<10

then s:=chr(ord('0')+a[k]}

else s:=chr(ord('A')+a[k]-10);

write (s) ;

end;

readln;

end.

2.04. r

num_to_str (, ), num . , num 4- , r |2, 16]. , 9, , , ... , F.

1 ()

, (r = 2,8,16), . ( r > 10) "". . q 10 , , , q-10 . , , . .

2 (QBasic)

, , ,

AS LONG, AS STRING, AS INTEGER, %, &, $ . "" . $ = A$ + CIF$ $ = CIF$+A$.

3 ()

, , r = 2 (. . 32 ). , , . ( , . .) . strlen string, h.

2_04.bas

REM r

DECLARE FUNCTION NToStr$(NUM&,R%)

CLS

INPUT " : ",N&

PRINT " : "

FOR J%=2 TO 16

PRINT " ";

J%,N&;"= ";NToStr$(N&,J%) NEXT J% END

FUNCTION NToStr$(NUM&,R%) A$="": MS=NUM& DO

CIF=M& MOD R% : '

IF CIF<10 THEN

A$=CHR$(ASC("0")+CIF)+A$ : ' ASCII ELSE

A$=CHR$(ASC("A")+CIF-10)+A$ END IF

&=(&-CIF)/R% : '

LOOP UNTIL M&=0 NToStr$=A$ END FUNCTION

2_04.

/* r */

#include <stdio.h>

# include <conio.h>

char *num_to_str (long num, char r) ;

void main (void) {

long N;

char j ;

printf ("\n ");

scanf ("%ld",&N);

printf (" :\n")

for(j=2; j<17; j++)

printf ("\n %2d = %s", j ,num_to_str (N, j ) ) ;

getch ( ) ;

}

char *num_to_str(long num, char r) {

static char a[33];

char cif, k, count=0; do {

cif=num % r;

/* */

if(cif<10) a[count++]='0'+cif;

/* ASCII, 10 */

else a[count++]='A'+cif-10;

/* ASCII, 9 */

num=num/r;

/* */ }

/* */

while (num != 0);

a[count--]=00;

for(k=0; k<=count/2; k++)

{ cif=a[count-k]; a[count-k]=a[k]; a[k]=cif; } return a; }

2_04.pas

program num to pos;

{ }

var

N:longint;

j:byte;

function num_to_str(num: longint;r:byte):string; var

a:string[33];

cif,k:byte; begin

a:='';

repeat

cif:=num mod r; { }

if (cif<10) then a:=chr(48+cif)+a

{ ASCII, 10 }

else a:=chr(65+cif-lO)+a;

{ ASCII, 9 }

num:=num div r; { }

until (num=0);

num_to_str:=; end; begin

write(' - ');

readln(N);

writelnf'Ero :']

for j :=2 to 16 do

writeln('no ',j:2,' = ',num_to_str(N,j));

readln; end.

2.05.

N (N < ). Pmin, N . ,

N=0 Pmin-2 (910 = 10012),

N-1000 Emin=S (100010 - 13319)

N, Pmin

N ( ).

C 1(o)

, . N 15 . , , 15 .

, , N . , , N-1, . . N 11. 2 N-I , , .

2 ()

perevod ( ) proba ( ). num_to_str, , , , .

3 (QBasic)

, . . .

2_05.bas

R

DECLARE FUNCTION perevod!(n%,p%,a%())

DECLARE FUNCTION proba! (a%(),k%)

DIM a%(16)

CLS

INPUT " [0,30000] : ",n%

FOR p%=2 TO n%-l

k%=perevod(n%,p%,a%()) : ' -

IF proba(a%(), k%) 0 THEN

PRINT "

FOR i = 0 k% - 1

PRINT TAB(0);%(i);"*";%; "^";k%-i;"+";

NEXT i

PRINT :

PRINT a% (0);"*";p%;"^";0

END

END IF

NEXT p%

END

FUNCTION perevod (n%,p%,a%())

REM n

R -

m%=n%

FOR i=0 TO 15

a%(i)=m% MOD p%

m%=(m%-a%(i))/p%

IF m%=0 THEN perevod=i: EXIT FUNCTION

NEXT i

END FUNCTION

FUNCTION proba (a%(),k%)

REM , k

REM - , proba=l

proba=l

FOR i=0 TO k%/2

IF a% (i)0a% (k%-i) THEN proba=0: EXIT FUNCTION

NEXT i

END FUNCTION

2_05.

/* */

#include <conio.h>

#include <stdio.h>

int perevod(int n, int p, int *a);

int proba(int *a, int k);

int i ;

main() {

int k,p,N,a[16];

clrscr () ;

scanf("%d",&N);

for (p=2; p<N; p++)

k=perevod(N, p, a); /* - */

if (proba(a,k)==0) continue;

printf(" = %d\n",p);

for(i=0; i<=k; i++)

printf("%d ",a[i]);

break;

getch(); }

int perevod(int n,int p,int *a) /*

- */

for(1=0; 1<1; 1++)

a[i]=n % ; n=n/;

if(n==0) return i;

}

int proba (int *a, int k)

/* , k

- , proba=l */ {

for(i = 0; i <= k/2; 1++)

if(a[i] != a[k-i]) return 0; return 1 ;

2_05.pas

program min_base;

{ }

uses Crt;

var

i, k, p,N: integer;

a:array [0..15] of integer;

function perevod(n,p:integer) :integer;

{ n

- }

begin

for i:=0 to 15 do

begin

a[i]:=n mod p;

n:=n div p;

if n=0 then

begin perevod:=i;

exit;

end;

end;

end;

function proba(k:integer):integer;

{ , k

- , proba=l }

begin

for i:=0 to k div 2 do

if a[i]<>a[k-i] then

begin proba:=0;

exit;

end;

proba:=1;

end;

begin clrscr;

writeln(' ');

writeln(' ');

readln(N);

for p:=2 to N do

begin

k:=perevod(N,p);

if proba(k)=0 then continue;

writeln(' = ',p);

for i:=0 to k do writeln(a[i]);

break;

end;

readln;

end.

2.06.

sum_dig(n), . n.

1 ()

, . - 10 .

2 (OBasic)

10. QBasic , , , 15 10 1, 2.

3 (, )

, do () repeat () .

2_06.bas

R

DECLARE FUNCTION SUMDIG (N&)

CLS

INPUT " ";&

K=SUMDIG(M&)

PRINT " = ";

END

FUNCTION SUMDIG(N&)

IF N&<0 THEN N&=-N& : '

RESULT=0

DO

RESULT=RESULT+(N& MOD 10&) : '

N&=(N&-(N& MOD 10&))/10& : '

LOOP WHILE N&<>O

SUMDIG=RESULT

END FUNCTION

2_06.

/* */

#include <stdlib.h>

int sum_digits(long N);

main()

}

long M;

printf ("\n : ");

scanf ("%ld", &M) ;

printf ("\n = %d", sum_digits (M) ) ;

getch ( ) ; }

int sum_digits (long N) {

int Result=0;

N=labs (N) ; /* */

do {

Result=Result+N%10; /* */

N=N/10; /* */ }

while (N != 0) ;

return Result;

}

2_06.pas

program sumdigits;

{ }

var

M:longint;

function sum_digits (N: longint) : integer;

const

Result : integer=0 ;

begin

if N<0 then N:=-N;

{ } repeat

Result :=Result+N mod 10; { }

N:=N div 10; { }

until N=0;

sum_digits : =Result;

end;

begin

write ( ' : ' ) ;

readln (M) ;

writeln(' =',sum_digits(M)! readln;

end.

2.07. ""

luck(n), [0, 999999]. true () i (, QBasic), "" , .

2_07.bas

REM ""

DECLARE FUNCTION LUCK(M AS LONG)

INPUT " ";N& IF LUCK(N&)=1 THEN

PRINT " - " ELSE

PRINT " " END IF END

FUNCTION LUCK(M AS LONG)

REM

REM LUCK=1

DIM A(6)

LUCK=0

IF M<0 OR M>999999 THEN

PRINT "luck : ":

EXIT FUNCTION

END IF

FOR i=0 TO 5

A(I)=M MOD 10 : '

M=(M-A(I))/10 : '

NEXT I

IF (A(0)+A(1)+A(2)=A(3)+A(4)+A(5)) THEN LUCK=1

END FUNCTION

2_07.

/* "" */

#include <stdio.h>

int luck(long M);

main() {

long N;

printf("\n ");

scanf("%ld",&N);

if (luck(N)==l)

printf("\n - ");

else

printf("\n "); getch(); }

int luck(long M)

/*

luck=l*/ {

int i ; char a[6];

if((M<0) || (M>999999)) {

printf("\nluck : ");

exit(0); }

for(i=0; i<6;.i++) {

a[i]=M % 10; /* */

M=M/10; /* */

}

if(a[0]+a[l]+a[2]=a[3]+a[4]+a[5]> return 1;

return 0; }

2_07.pas

program lucky_ticket;

{ ""

var

N:longint;

function luck(M:longint):boolean;

{

luck=true }

var

i:integer;

a:array [1..6] of byte;

begin

if (M<0) or (M>999999) then begin

writeln('luck : ');

exit;

end;

for i:=l to 6 do

begin

a[i]:=M mod 10; { }

M:=M div 10; { }

end;

luck:=(a[l]+a[2]+a[3])=([4]+[5]+[6]);

end;

begin

writeln(' ');

readln(N);

if luck(N) then

writeln(' - ') else

writelnt'HeT ');

readln;

end.

2.08.

num_digits (n), . . : num_digits (1998) =3.

1 ()

10 , ,

. . , , . , .

2 (, )

num_digits . , , . , .

2_08.bas

R

DECLARE FUNCTION NumDig!(N&)

CLS

INPUT " : ", N&

PRINT " = ";NumDig(N&)

END

FUNCTION NumDig(N&)

REM N

DIM d(10) : '

d(i)=0

IF N&<10 THEN NumDig=l: EXIT FUNCTION

DO

k%=N& MOD 10 : '

d(k%)=d(k%)+l : ' , k

N&=(NS-k%)/10 : '

LOOP UNTIL N&=0

FOR k%=0 TO 9 : '

IF d(k%)<>0 THEN s=s+l : ' d(i)=0, i

NEXT k%

NumDig=s

END FUNCTION

2_08.

/* */

#include <stdio.h>

#include <conio.h>

int num_digits(long N);

main() {

long N;

printf("\n - ");

scanf("%ld",&N);

printf("\ = %d ",

num_digits(N));

getch(); }

int num_digits(long N)

/* N */ {

char d[10]={0,0,0,0,0,0,0,0,0,0}; /* */

int k,s=0;

if(N<10) return 1;

while (N) {

d[N % 10]++; /* */

N=N/10; /* */ }

/* */

for(k=0; k<10; k++)

if(d[k]) s++;

return s; }

2_08.pas

program NumDigits;

{ }

var

N:longint;

function num_digits(N:longint):byte;

{ N } var

d:array [0..9] of byte; { }

s,k:byte;

begin

if N<10 then num_digits:=1

else begin

for k:=0 to 9 do d[k]=0; { }

s: =0 ;

while N<> 0 do begin

inc(d[N mod 10]); { }

N := N div 10; { }

end;

{ }

for k:=0 to 9 do

if(d[k]<>0) then inc(s);

num_digits:=s;

end;

end;

begin

write(' : ');

readln(N);

writeln(' = ',num_digits(N));

readln;

end.

2.09.

digit_in_pos (n,i), n i n. 0 "" ( ), . :

n=1985

0- 5;

1- 8;

2- 9;

3- 1;

digit_in_pos , 1- .

2_09.bas

R

DECLARE FUNCTION DIGINPOS(N AS LONG,J AS INTEGER)

INPUT " : ";&

FOR K%=0 9

DIGIT=DIGINPOS(M&,%)

PRINT " ";%;" ",DIGIT NEXT K%

END

FUNCTION DIGINPOS(N AS LONG,J AS INTEGER)

REM N j

N1&=N

FOR K%=0 TO J

RESULT=N1& MOD 10

N1&=N1&-RESULT)/10 NEXT K%

DIGINPOS=RESULT

END FUNCTION

2_09.

/* */

#include <stdlib.h>

int digit_in_pos(long N,int j);

main () {

long M;

int k;

printf(" : ");

scanf("%ld",&M);

for(k=0; k<10; k++)

printf("\nB %d %d",k,digit_in_pos(M, k)) ;

getch(); }

int digit_in_pos(long N,int j)

/* N j */ {

int Result,k;

for (k=0; k<=j; k++) {

Result=N % 10; N=N/10;

}

return Result; }

2 09.pas

program DigitlnPos;

{ }

var

M:longint;

k:integer;

function digit_in_pos(N:longint;j:integer):integer;

{ N j }

var

Result,k:integer;

begin

for k:=0 to j do

begin

Result:=N mod 10;

N:=N div 10;

end;

digit_in_pos:=Result;

end;

begin

writeln(' :');

readln(M);

for k:=0 to 9 do

writeln(' ',k,' ',digit_in_pos(M,k));

readln;

end.

2.10.

, [0, 999], N ( < N < 27).

2_10.bas

REM

INPUT " 0 27 : ",n

PRINT " , ";n;" :"

FOR a2%=0 9

FOR a1%=0 TO 9

FOR a0%=0 TO 9

IF a2%tal%+aO%=n THEN

PRINT USING "####";100*a2%+10*al%+a0%;

END IF

NEXT a0%,al%,a2%

END

2_10.c

/* */

#include <stdio.h>

#include <conio.h>

main () {

int a2,al,a0,n;

printf("\n 0 27 : ");

scanf("%d",&n);

printf("\n , %d :\n",n);

for(a2=0; a2<10; 2++)

for(al=0; al<10; al++)

for(a0=0; a0<10; a0++)

if(a2+al+a0==n) printf("%4d",100*a2+10*al+a0);

getch(); }

2_10.pas

program numbers;

{ }

var

2,al,aO,n:integer; begin

write(' 0 27 : ');

readln(n);

writeln(' , ',n, ' :');

for a2:=0 to 9 do

for al:=0 to 9 do

for a0:=0 to 9 do

if (a2+al+a0)=n then write(100*a2+10*al+a0:4);

readln;

end.

2.11.

num_to_3tr(n), (|n| < 1000). , . :

num_to_str(-87) = " ".

1 ()

, . , , 1000 "", ""....." ". ("", "", ..., ""), ("", "", "") ("", ""..... "") . , , "".

2 ()

num_to_str string.h. Result . static, Result . , . static Result , , N=999.

3 (QBasic)

QBasic DATA-READ . numl$, num2$ num$ , . ( ) ( ) . , 2_11a.bas, numis, num2$ num3$ DIM SHARED.

4 ()


num1. num2 num3 "".

2_11.bas

R

DECLARE FUNCTION NumToStr$(m%,numl$(),num2$(),num3$())

CLS

DATA "","","","","","","",""

DATA "","","","","" .

DATA "","","",""

DATA "","",""

DIM numl$ (20)

FOR k=0 TO 19: READ numl$(k): NEXT k

DATA " "," "," "," "

DATA " "," "(" "," "

DIM num2$(8)

FOR k=0 TO 7: READ num2$(k): NEXT k

DATA "","CTO "," "," "," "," "

DATA " "," "," "," "

DIM num3$(10)

FOR k=0 TO 9: READ num3$(k): NEXT k

INPUT " -999 999: ",n%

PRINT n%;"= ";NumToStr$(n%,numl$(),num2$(),num3$())

END

FUNCTION NumToStr$(m%,numl$(),num2$() , num3$())

IF m%=0 THEN NumToStr$=numl$(0): EXIT FUNCTION

IF m%<0 THEN m%=-m%: Res$=" " : '

dlg100%=m%\100 : '

Res$=Res$+num3$(dig100%) /

m%=m%-100*dig100% : "

IF m%=0 THEN NumToStr$=Res$: EXIT FUNCTION

IF m%<20 THEN NumToStr$=Res$+numl$ (m%) : EXIT FUNCTION

dig10%=m%\10 :' , dig10 >=20

Res$=Res; +num2$ (diglO%-2)

digl%=m% MOD 10 :'


IF digl%<>0 THEN Res$=Res$+numl$(digl%)

NumToStr$=Res$

END FUNCTION

2_11a.bas

R

DECLARE FUNCTION NumToStr$(m%)

CLS

DATA "","","","","","","", ""

DATA "","","","",""

DATA "","","",""

DATA "","",""

DIM SHARED numl$(20)

FOR k=0 TO 19: READ numl$(k): NEXT k

DATA " "," "," "," "

DATA " "," "," "," "

DIM SHARED num2$(8)

FOR k=0 TO 7: READ num2$(k): NEXT k

DATA ""," "," "," "," "," "

DATA " "," "," "," "

DIM SHARED num3$(10)

FOR k=0 TO 9: READ num3$(k): NEXT k

INPUT " -999 999: ",n%

PRINT n%;"= ";NumToStr$(n%) '

END

FUNCTION NumToStr$(m%)

IF m%=0 THEN NumToStr$=numl$(0): EXIT FUNCTION

IF m%<0 THEN m%=-m%: ResS-'' " : '

dig100%=m%\100 : '

Res$=Res$+num3$(dig!00%) :'

m%=m%-100*diglOO% : '

IF m%=0 THEN NumToStr$=Res$: EXIT FUNCTION

IF m%<20 THEN NumToStr$-Res$-t-numl$ (m%) : EXIT FUNCTION diglO%=m%\10 :

' ,

dig10>=20 R@s$=R.es$-l-num2$ (diglO%-2) :'

diglS-^m* MOD 10


IF digl%<>0 THEN Res$=Res$+numl$(digll)

NumToStr$=Res$ END FUNCTION

2_11.

/* */

#include <stdio.h>

#include <conio.h>

#include <string.h>

char *num_to_str(long m) ;

main() {

long n;

clrscr() ; m:

printf(" : ");

scanf("%ld",&n);

printf("\n%ld = %s",n,num_to_str(n));

goto m;

getch(); }

char *num_to_str(long m)

/* */ {

char *numl[]={"","","","",

"","","","","","",

"","","",

"","",

"","","", "",

" " }';

char *num2[]=1

" "," "," ",

" "," ",

" "," "," "};

char *num3[]=("",

" "," "," "," ",

" ", " "," "/' "/' "} ;

static char Result[50]="";

int digl,dig10,dig100;

if(m==0) return numl[0]; Result[0]=0x0;

if(m<0) /* */ {

m=-m;

strcpy(Result," ") ; }

diglOO=m/100; /* */

strcat(Result,num3[diglOO]);

/* */

m=m-100*dig100;

/* */

if (m=0) return Result;

/* - */

if(m<20) /* 20 */

{

strcat(Result,numl[m]);

return Result; }

diglO=m/10;

/* , diglO >=20 */

strcat(Result,num2[diglO-2]);

digl=m % 10;

/* */

if(digl != 0)

strcat(Result,numl[digl]);

return Result; }

2_11.pas

program nd_10;

{ )

uses Crt;

var

n:longint;

function num_to_str(m:longint):string;

{ }

label ret;

"const

numl :array [0. .19] o£ string= (

1 ', '', '', '', '', '', '', '','','','','', '','','', '','','', '','');

num2:array [2..9] of string=(

' ',' ',' ',' ', ' ',' ',' ',' ');

num3:array [0..9] of string = ('',

' ',' ',' ',' ',' ', ' ',' ',' ',' ');

var

digl,diglO,diglOO: byte;

Result:string;

begin

if m=0 then begin

Result:=numl[0];

goto ret;

end;

Result:='';

if m<0 then { }

begin

m:=-m;

Result:=' '; end;

diglOO:=m div 100; { }

Result:=Result + num3[diglOO];

{ }

m:=m-100*diglOO;

{ }

if m=0 then goto ret;

{ - }

if m<20 then begin

{ 20 }

Result:=Result+numl [m] ;

goto ret;

end;

dig10:=m div 10;

{ , diqlO >=20 }

Result:=Result+num2[dig10] ;

digl:=m mod 10; { }

if dig100 then Result:=Result + numl[digl];

ret:

num_to_str:=Result;

end;

begin

clrscr;

write{' : ');

readln (n) ;

writeln(n, ' = ' ,num_to__str (n) ) ;

readln; end.

2.12.

sum_bits(n), . .

1 ()

, . -1 32 .

2 (QBasic)

QBasic , 2 J&, . J&=&H80000000 , . . "". N , , 30- .

2_12.bas

R

INPUT " N"/ N&

Js=&H40000000

RESULT=0

IF N&<0 THEN RESULT=1 : ' 1

FOR K=31 TO I STEP -1

IF N& AND J& THEN RESULT=RESULT+1 : ' k-

J&=J&/2 : ' NEXT PRINT RESULT END

2_12.

/* */

#include <stdlib.h>

int sum_bits(long N) ;

main () {

long M;

printf(" : ");

scanf("%ld",&M);

printf("\n = %d",sum_bits(M));

getch(); }

int sum_bits(long N)

/* N */

{

int Result=0,k;

unsigned long j=0x80000000;

for(k=31; k >= 0; k--) {

if(j & N) Result++;

/* k- */

j=j >> 1; }

return Result;

}

2_12.pas

program sumbits;

{ }

var

M:longint;

function sum bits(N:longint):integer;

const

Result:integer=0;

j:longint=$80000000; var

k:integer; begin

for k:=31 downto 0 do

begin

if (N and j)<>0 then inc(Result);

{ k- }

j:=j shr 1;

end;

sum_bits:=Result;

end;

begin

write(' : ');

readln(M);

writeln(' = ',sum_bits(M));

readln; end.

2.13.

lef t_bit (n), . . 0 31.

1 ()

( ) . , . , 30- . ( » ), shr, QBasic 2.

2_13.oas

R

DECLARE FUNCTION LeftBit!(N&)

INPUT " : "/Ms

IF M&=0 THEN PRINT "D " : END

PRINT " ";

LeftBit(M£)

END

FUNCTION LertBil(N&)

REM N

N=0, LeftBit=-l

LeftBit=-l

j&=&H40000000

FOR k=30 TO 0 STEP -1

IF (j& AND N&) THEN ' k- LeftBit-k EXIT FUNCTION

END IF

j&=j&/2 NEXT k

END FUNCTION

2_13.

/* */

#include <stdlib.h>

int left_bit(long N) ;

main() {

long M;

printf("\n : ");

scanf("%ld",&M);

if(M==0) {

printf("\n B ");

exit(0); }

printf("\n %d", left__bit(M));

getch(); }

int left_bit(long N)

/* N

N=0, -1 */ {

int k;

long j=0x80000000;

for(k=31; k >= 0; k--) {

if(j & N) return k;

j=j >> 1;

}

return -1; }

2_13.pas

program LeftBit;

{ }

var

M:longint;

function left_bit(N:longint):byte;

{ N N=0, -1 }

var

k:byte; const

j:longint=$80000000;

begin

left_bit:=-l;

for k:=31 downto 0 do

begin

if (j and N)<> 0 then begin

left_bit:=k;

exit;

end;

j:=j shr 1;

end;

end;

begin

write(' : ');

readln(M);

if M=0 then writeln('B ')

else

writelnf' ', left_bit(M));

readln; end.

2.14.

max_bits(n), . . :

n = 3310 = 100012 max_bits (33) =1

n = 2810 = 111002 max_bits(28) =3

1 ()

, . , , , , -1 ( OXFFFFFFFF) 32 . , . QBasic .

2 (QBasic)

, 2.

3 ()

itoa . 1 . 1 , - . (N 0) . , , ( 32 ). , . , .

2_14.bas

R

DECLARE FUNCTION MaxBitS!(N&,s$)

CLS

INPUT " : ",&

k%=MaxBits(M&,a$)

PRINT " :"

PRINT a$

PRINT " = "; k%

END

FUNCTION MaxBits(NS,s$)

REM N

REM (MaxBits = )

IF N&=0 THEN MaxBits=0: s$="0": EXIT FUNCTION

FOR j=0 TO 31 : ' s$

IF N& AND &H1 THEN

kBits=kBits+l : '

IF kBits>max THEN max=kBits : '

s$="l"+s$ : ' ELSE

kBits=0 : '

s$="0"+s$ : ' END IF

N&=(N&-k)/2 : '

NEXT j

MaxBits=max

END FUNCTION

2_14.

/* */

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

int max_bits(long N);

main ()

{

long M;

char str[33];

clrscr () ;

printf("\n : ");

scanf("%ld",&M);

printf(" :");

ltoa(M,str,2);

printf("\n%32s",str);

printf("\n = %d",

max_bits(M));

getch(); }

int max_bits(long N)

/* N

(max_bits = ) */

{

int k_bits=0,max=0;

char s[33];

if(N==0) return 0;

while (N) /* s$ */

{

if(N & 0x80000000) /* 1 */

{

/* */

k_bits++;

if(k_bits>max) max=k_bits; /* */

}

else

k_bits=0; /* , = 0 */

N=N << 1 ; /* 1 */

}

return max; }

2_14.pas

program MaxBits;

{ }

uses Crt;

var

M:longint;

str:string[33];

function max_bits(N:longint)rshortint;

{ N

(max_bits = ) }

var

k bits, max:byte;

begin

k_bits:=0;

max:=0;

if N=0 then

begin

max_bits:=0;

exit;

end;

while N<>0 do { s$ }

begin

if (N and $00000001)00 then { 1 }

begin

str:='l'+str;

inc(k_bits); { }

if k_bits>max then max:=k_bits end else begin

str:='0'+str;

k_bits:=0; { , = 0 }

end;

N:= N shr 1; { 1 }

end;

max_bits:=max; end;

begin clrscr;

write(' : ') ;

readln(M);

writeln(' = ',max_bits(M));

readln;

end.

2.15.

"" n- . :

01011010

00101110

-------------

=***=*== ""=4

rasst (ni,n2), , 32- . "" nl 2.

1 ()

XOR, 2, . .

2_15.bas

REM

DECLARE FUNCTION NumToBin$(N&)

DECLARE FUNCTION SumBits!(N&)

CLS

INPUT " :",N1&

INPUT " :",N2&

PRINT " :"

PRINT NumToBin$(N1&) : ' N1

PRINT NumToBin$(N2&) : ' N2

PRINT " = "; SumBits(N1& XOR N2&)

END

FUNCTION NumToBin$(N&)

REM N

$="": =&1

IF N&=0 THEN NumToBin$="0": EXIT FUNCTION

FOR J=0 TO 30

IF N& AND THEN s$="l"+S$ ELSE s$="0"+s$

K=K*2 : ' 1

NEXT J

IF N&<0 THEN s$="l"+s$ : '

NumToBin$=s$

END FUNCTION

FUNCTION SumBits (N&)


J&=&H40000000

RESULT=0

IF N&<0 THEN RESULT=1

FOR K=31 TO 1 STEP -1

IF N& AND J& THEN RESULT=RESULT+1

J&=J&/2 NEXT

SumBits=RESULT END FUNCTION

2_15.c

/* */

#include <stdlib.h>

int rasst(long nl, long n2);

char s [ 4 0];

main() {

long M1,M2;

printf("\n : ");

scanf("%ld %ld",&Ml,&M2);

printf("\nMl =%32s",ltoa(Ml,s,2)); /* N1*/

printf("\nM2 =%32s",ltoa(M2,s,2)); /* N2*/

printf("\nXOR=%32s",ltoa(MlAM2,s,2));

printf("\n = %5d",rasst(M1,M2));

getch();

}

int rasst(long nl,long n2)

{

int Result=0,k;

long j=0x00000001;

for(k=0; k <32; k++)

{

/* */

if((j & nl)!=(j & n2)) Result++;

j=j « 1; /* */

}

return Result; }

2_15.pas

program distance;

{ }

var

Ml,M2:longint;

function rasst(nl,n2:longint):byte;

const

Result:byte=0 ;

j:longint=$00000001;

var

k:byte;

n3:longint;

begin

n3:=nl xor n2;

for k:=0 to 31 do { }

begin

if (j and n3)<>0 then inc(Result);

j:=j shl 1;

{ }

end;

rasst:=Result; end; begin

write(' : ');

readln(Ml,M2);

writeln(' = ',rasst(Ml,M2));

readln; end.

2.16.

invert (n), . , n . . :

invert(314159) =951413

invert(-2718) = -8172

1 ()

, , , , . , 10 .

2_16.bas

R

DECLARE FUNCTION invert!(N&)

CLS

INPUT " : ",&

PRINT " ";invert(MS)

END

FUNCTION invert(N&)

Res&=0: sign=SGN(N&) : '

IF N&<0 THEN N&=-N& '

DO

k%=(N& MOD 10) : '

Res&=Res&*10+k% : '

N&=(Ns-k%)/10 : '

LOOP UNTIL N&=0

invert=Res&*sign : '

END FUNCTION

2_16.

/* */

#include <stdlib.h>

long invert(long N);

main()

{

' long M;

printf("\n : ");

scanf("%ld",&M);

printf("\n %ld",invert(M));

getch();

}

long invert(long N) {/* */

long Result=0, sign=l;

/* */

if(N<0)

{ N=-N; sign=-l;}

while (N!=0) {

Result=Result*10 + (N % 10);

/* */

N=N/10;

/* */

}

return Result*sign; /* */ }

2._16.pas

program rotate;

{ }

var

N:longint;

function invert(N:longint):longint; const

Result:longint=0; sign:shortint=l;

begin

if N<0 then { }

begin N:=-N;

sign:=-l;

end;

while (N<>0) do begin

Result:=Result*10+(N mod 10);{ }

N:=N div 10; { }

end;

invert:=Result*sign; { }

end;

begin

write(' : ');

readln(N);

writeln(' : ',invert(N));

readln;

end.

2.17.

N = a1a2... ak , , , NI ak...a2a1. , a1> . , 1881 , 1812 . paiindrom(n), . true () 1 (QBasic, ), .

1 ()

, palindrom invert. N invert (N). , .

2 ()

, . STR$ QBasic, Itoa , str . 2_17a.pas . , , .

3 (QBasic)

2_l7.bas .

, STR$ . . MID$, , .

2_17.bas

REM ?

DECLARE FUNCTION palindrom!(NS)

LS

DIM noyes$(2)

noyes$(0)=""

INPUT " : ",&

PRINT " - "; noyes$(palindrom(M&));" "

END

FUNCTION palindrom(N&)

REM N - , palindrom = 1

palindrom=l

IF N&<10 THEN EXIT FUNCTION : ' -

s$=STR$(N&) : '

k%=LEN(s$)

FOR j=2 TO l+k%/2 : '

IF MID$ (s$, j,l)OMID$ (s$,k%+2-j,l) THEN palindrom=0

NEXT j

END FUNCTION

2_17.

/* ? */

#include <stdio.h>

#include <conio.h>

int palindrom(long N);

main () {

long M;

printf("\n : ");

scanf("%ld",&M);

if (palindrom(M)) printf("\n - ");

else printf("\ - ");

getch(); }

int palindrom(long N)

{

/* N - , palindrom = 1 */

int j,k=l;

char digit[10];

if(N<10) return 1; /* - */

for(j=0; j<10; j++)

{

/* */

digit[j]=N%10;

N=N/10; /* */

if(N!=0) k++;

}

for(j=0; j<=k/2; j++) /* */

if(digit[j]!=digit[k-j-1]) return 0;

return 1;

}

2_17.pas

program Palindroms;

{ ? }

var

: longint; function

palindrom(N:longint):boolean;

{ N - , palindrom = true }

var

j,k:integer;

digit:array [0..9] of byte;

begin

palindr om: =t rue ;

if N<10 then exit; { - }

k:=0;

for j:=0 to 9 do { }

begin

digit[j]:=N mod 10; { N }

N:=N div 10; { }

if(N<>0) then k:=k+l; { N }

end;

for j:=1 to (k div 2) do { }

if digit[j]odigit[k-j] then palindrom:=false;

end;

begin

write(' : ');

readln(M);

if palindrom(M) then

writeln(' - ') else

writeln(' ');

readln;

end.

2_17a.pas

program Palindroms;

{ ? }

var

: longint; const

noyes:array [false..true] of string=('', '');

function palindrom(N:longint):boolean;

{ N - , palindrom = true }

var

j,k:byte ;

s:string[16];

begin

palindrom:=true;

if N<10 then exit; { - }

str(N,s); { }

k:=length(s) ;

for j:=l to (k div 2) do { }

if s[j]<>s[k+l-j] then palindrom:=false; end; begin

write(' : ');

readln();

writeln(' - ',noyes[palindrom()],' ');

readln;

end.

2.18.

, , . , k (k < 5).

1 ()

[1000, 9999] a1a2a2a1, 90 .

2_18.bas

R ,

DECLARE FUNCTION palindrom!(N&)

CLS

FOR Al=l TO 9: FOR A2=0 TO 9

M&=(((A1*10)+A2)*10+A2)*10+A1

M2&=M&*M&

IF palindrom(M2&) THEN PRINT M&,M2& NEXT A2, A1 END

FUNCTION palindrom (N&)

REM N - , palindrom = 1

palindrom=l

IF N&<10 THEN EXIT FUNCTION : ' -

s$=STR$(N&) : '

k%=LEN(s$)

FOR j=2 TO l+k%/2 : '

IF MID$ (s$, j,l)OMID$(s$,k%+2-j,l) THEN palindrom=0 NEXT j

END FUNCTION

2_18.

/* , */

#include <stdio.h>

#include <conio.h>

int palindrom (long N);

ma in ()

{

long M,M2;

int al,a2;

for(al=l; al<10; al++)

for(a2=0; a2<10; a2++)

{

M=(((al*10)+a2)*10+a2)*10+a1;

M2=M*M;

if(palindrom(M2))

printf ("\=%1d =%1d",,2) ; }

getch();

int palindromflong N)

{/* N - , palindrom = 1 */

int j,k=l;

char digit[10];

if(N<10) return 1; /* - */

for(j=0; j<10; j++) /* N */

{

digit[j]=N % 10; N=N/10;

if(N!=0) k++;

}

for(j=0; j <= k/2; j++) /* */

if(digit[j]!=digit[k-j-1])

return 0;

return 1; }

2_18.pas

program sqr_pal;

{ , }

var

M,M2:longint;

al,a2:integer;

function palindrom(N:longint):boolean; { N - , palindrom = true }

var

j,k:integer;

digit:array [0..9] of byte; begin k:=l;

palindrom:=true;

if N < 10 then exit; { - } for j : =0 to 9 do { ^ N }

begin

digit[j]:=N mod 10; N:=N div 10;

if N00 then inc(k);

end;

for j:=0 to k div 2 do { }

if digit[j]odigit[k-j-1] then palindrom:=false; end;

begin

for al:=l to 9 do

for a2:=0 to 9 do begin

M:=(((al*10)+a2)*10+a2)*10+al;

M2:=M*M;

if palindrom(M2) then

writeln('=',M,' =',2);

end;

readln; end.

2.19.

, N :

N1+i = a(i,k)3+ a(i,k-l)3+ ... + a(i,0}3

a (i,k), a(i,k-i),... , a(i,0) NL (N! = N-J, i > j).

1 ()

1999999999 , 6562. Q 6562 , . NI Q [Ni] i, 0. , Nj, Q[NJ] - . , , Q.

2 (QBasic)

, , MASS. .

3 ()

, , . :

1 > 1 ( ),

136 -> 244 > -136 ( ),

55 -> 250 -> 133 -> ( ).

2_19.bas

R

DECLARE FUNCTION nkub& (N&)

DIM MASS(100) : '

CLS

INPUT " - ", N&

=0: MAS&(0)=N& ml: =+1 : '

MASS(K)=nkub&(MASS(K-l))

PRINT " =";," N =";MAS&(K)

FOR 1=0 -1 : '

IF MAS&(I)=MASS(K) THEN GOTO m2

NEXT I

GOTO ml m2: PRINT " ": PRINT " ";1

PRINT " ";-1

END

FUNCTION nkub&(N&)


DIM S AS INTEGER, M AS LONG S=0: M=N& WHILE M<>0

D=M MOD 10: M=(M-D)/10 : '

S=S+D*D*D : '

WEND nkub&=S

END FUNCTION

2_19.c

/* */

#include <stdio.h>

#include <conio.h>

long n_kub(long N);

long N;

int i=l,Q[6562];

main ()

{

clrscr();

printf("\n ");

scanf("%ld",&N); Q[N-l]=i; ml:

printf ("\n a=%2d N=%ld", i,N) ;

N=n_kub(N);

if(Q[N-1]!=0) /* He ?*/ {

printf("\nHa %d N=%ld",i+l,N);

getch ();

exit(0) ;

}

Q[N-l]=i++; /* Q*/

goto ml;

}

/ *-------------------------------------* /

long n_kub(long N)

{

/* */

int k;

long s=0; while (N)

{

k=N%10;

N=N/10; /* */

s+=k*k*k; /* */

}

return s;

}

2_19.pas

program loop cub

{ }

uses Crt; label ml; var

N:longint;

Q:array [1..6562] of integer; i:integer;

function n_kub(N:longint):integer;

{ - }

var

s,k:integer; begin s:=0;

while (N<>0) do begin

k:=N mod 10;

N:=N div 10; { }

s:=s+k*k*k; { }

end;

n kub:=s;

end;

begin clrscr;

write(' ');

readln(N);

for i:=l to 6562 do Q[i]:=0;

{ Q}

i:=l;

Q[N]:=1;

ml:

writeln('ar=',i:2, ' N=',N);

N:=n_kub{N);

if Q[N]<>0 then { He ?}

begin

writeln('Ha ',1+1:2,' ');

readln;

halt;

end;

inc(1); { )

Q[N]:=i; { Q }

goto ml;

end.

2.20.

, N . :

128 = 2*2*2*2*2*2*2 17 =

1 ()

N k = 2, 3, ... , N . , k k > N/2. j , . J , .

2_20.bas

R

CLS

& = 2: J% = 0

INPUT " : ", &: 1& = Ms / 2

PRINT M&; "=";

Ml:

IF MS MOD K&=0 THEN

J=l: M&=M&/K&: PRINT K&;

IF M&<>l THEN PRINT "*";

ELSE K&=K&+1

END IF

IF K&<=M1& THEN GOTO Ml

IF J=0 THEN PRINT " "

END

2_20.

/* */

#include <stdio.h>

#include <conio.h>

main() {

long M,M1,k=2;

char j=l;

printf("\n : ");

scanf("%ld",&M);

Ml=M/2;

printf("\n%ld=",M);

ml:

if (M % k=0) {

j=0;

M=M/k;

printf("%ld",k) ;

if(M!=l)printf("*"); }

else k++;

if(k<=Ml) goto ml;

if(j==l)

printf(" ");

getch(); }

2_20.pas

program razlojenie;

{ }

var

M,Ml,k:longint ;

j:boolean;

IF K&<=Ml& THEN GOTO Ml

IF J=0 THEN PRINT " "

END

2_20,

/* */

#include <stdio.h>

#include <conio.h>

main ()

{

long M,M1,k=2;

char j=l;

printf("\n : ");

scanf("%ld",&M);

Ml=M/2;

printf("\n%ld=",M);

ml:

if(M % k==0)

{

j=0; M=M/k;

printf("%ld",k) ;

if(M!=l) printf("*"); }

else k++;

if(k<=Ml) goto ml;

if(j==l) printf (" ");

getch();

}

2_20.pas

program razlojenie;

{

}

var

,1,k:longint;

j:boolean;

label 1;

begin

j:=true; k: =2 ;

write(' : ');

readln(M);

M1:=M div 2;

write(M,' = ');

1:

if(M mod k)=0 then begin

j:=false;

M:=M div k;

write (k) ;

if Mol then write ('*');

end

else k:=k+l; if k<=Ml then goto 1;

if j then write(' '};

readln; end.

2.21. ""

prime (), . true () 1 (, QBasic), . false .

1 ()

"" , N 2, 3, 5, 7, 9, ..., 2*p+l ( (2*p+l)2 < N). , N . , , 9 15, 3. , 2, 3, 5, 7, 11, 13, ... (. 2.22).

2_21.bas

DECLARE FUNCTION prime!(N&)

REM

CLS

INPUT " : ", &

IF prime (M&)=1 THEN

PRINT " - " ELSE

PRINT " - "

END IF

END

FUNCTION prime(N&)

REM N - , prime = 1

DIM j AS LONG

IF N&<4 THEN GOTO Ml : ' 2, 3 -

IF N& MOD 2=0 THEN GOTO MO : ' -

REM

FOR j=3 TO SQR(N&)+1 STEP 2

IF N& MOD j=0 THEN GOTO MO NEXT j

Ml: prime=l: EXIT FUNCTION M0: prime=0 END FUNCTION

2_21.

/* */

#include <stdio.h>

#include <math.h>

int prime(long N) ;

main() {

long M;

char *a[]={"",""};

printf("\n : ");

scanf("%ld",SM);

printf("\n - %s", a[prime(M)]);

getch(); }

int prime(long N)

{

/* N - , prime = 1 */

long kmax,j;

if(N<4) return 1; /* 2, 3 - */

if(N % 2==0) return 0; /* - */

/* */

for(j=3; j*j<=N; j+=2)

if( N % j==0) return 0; return 1;

2_21.pas

program prostota;

{ }

var

:longint; const

a:array [0..1] of string=('','');

function prime(N:longint}:byte;

{ N - , prime = 1 }

var

j:longint; label ml;

begin

prime:=1;

if N<4 then exit; { 2, 3 - }

prime:=0;

if N mod 2=0 then exit; { - }

j:=3;

{ }

ml:

if N mod j=0 then exit;

j:=j+2;

if j*j<=N then goto ml;

prime:=1; end; begin

writeln (' : ');

readln();

writeln(' - ', a[prime()]);

readln; end.

2.22.

,

. 2, 3, 4, ... , 1000. , 2, , . . , 2. , , 3, , . . , 3. 5, 7 . . . , , , . , maxk .

1 ()

is, i- 2*1+3 1, 1- "", 0 . 3 is. " " , 5. "" is . . is , isi.

2 ()

(set). , , . ( ), ( ), ( in). ( 255).

2_22.bas

DEFINT A-Z CLS

=500 DIM ISI(MAXK+1)

FOR I=0 -1: IS1(I)=1: NEXT I

FOR I=0 TO MAXK IF ISI(I)=l THEN

' ""

PRIME=I+I+3: '

N=N+1: '

PRINT USING "#####";

PRIME; : '

K=I+PRIME: ' ""

WHILE <=A

REM "" ,

PRIME IS1(K)=0: K=K+PRIME

WEND

END IF

NEXT I

PRINT

PRINT " ";+1; " ";

PRINT " "; N; " "

END

2_22.

#include <stdio.h>

#include <conio.h>

#define maxk 500

main() {

int pr_num,n=0;

char is[maxk+1];

register int i,k;

clrscr () ;

for(i=0; i<=maxk; i++) is[i]=l; /* " " */

for(i=0; i<=maxk; i++)

{if(is[i]==l) /* "" */

{ pr_num=i+i+3; /* */

n++; /* */

printf("%5d",pr_num); /* */

k=i+pr num; /* "" */

while (<:=mk)

{

/* "" , pr_num */

is[k]=0; k+=pr_num;

}

}

}

printf("\n %d ", maxk+1);

printf(" %d ", n);

getch();

}

2_22.pas

program sievel;

uses Crt;

const

maxk=500;

n:integer=0;

var

is: array [0..maxk] of byte;

i,k,prime:integer;

begin clrscr;

for i:=0 to maxk do is[i]:=1; { " " }

for i:=0 to maxk do

begin

if is[i]=l then { "" }

begin

prime:=i+i+3; { }

inc(n); { }

write(prime:5); { }

k:=i+prime; { "" }

while k<=maxk do

begin { "" , pr_num }

is[k]:=0; inc(k,prime); end end; end;

writeln;

write(' ',maxk+l, ' ');

writeln(' ',n,' ');

readln;

end.

2_22a.pas

program sieve; uses Crt; const

maxN=255; ( 255} var

primes: set of 2..maxN;

i,j:integer;

begin clrscr;

primes:=[2..maxN]; { }

for i:=2 to maxN do

if i in primes then { i }

begin

write (i:5); { }

for j : =1 to maxN div i do

primes:=primes-[i*j]; { , i}

end;

readln;

end.

2.23.

1742 . , , . , . , , , . , , . , 2000 . 1 , .

, , , . , .

1 ()

k + (n - k). ,

. prime, . , k = 2, , /2.

2_23.bas

DECLARE FUNCTION prime!(N&)

REM CLS

INPUT " : ",& IF prime (M&-2)=1 THEN

PRINT M&; "=2-f ";M&-2

FOR j&=l TO M&/2 STEP 2

IF prime(j&)=l AND prime (M&-J&)=1 THEN

PRINT M&;"=";j&;"+";M&-j&

END IF

NEXT j& END

FUNCTION prime (N&)

DIM j AS LONG

IF N&<4 THEN GOTO Ml

IF N& MOD 2=0 THEN GOTO MO

FOR j=3 TO SQR(N&)+1 STEP 2

IF N& MOD j=0 THEN GOTO MO

NEXT j

Ml: prime=l: EXIT FUNCTION MO: prime=0

END FUNCTION

2_23.

/* */ tinclude <stdio.h> linclude <math.h> int prime(long N) ; main()

{

long M,j;

clrscr () ;

printf("\n : ");

scanf("%ld",&M);

if (prime(M-2) ) printf ("%ld== 2+%ld\t",M,M-2) ;

for(j=l; j <= M/2; j+=2)

if(prime(j) && prime(M-j)) printf("!ld=l31d+%ld\t",

M, j, M-j);

getch(); }

int prime(long N) {

long kmax,j;

if(N<4) return 1;

if(N%2==0) return 0;

kmax=sqrt(N)+0.5;

for(j=3; j<=kmax; j+=2)

if(N%j==0) return 0; return 1; }

2_23.pas

program razlojenie;

{ }

var

M:longint;

j:integer; label m2;

function prime(N:longint):byte; var

j:longint; label ml; begin

prime:=l;

if N<4 then exit;

prime:=0;

if N mod 2=0 then exit; j:=3;

ml: if N mod j=0 then exit;

J:=J+2;

if j*j<=N then goto ml;

prime:=1; end; begin

write (' : ');

readln(M);

if prime(M-2)=l then writeln(M,'+',M-2);

j:=3; m2:

if (prime(j)=l) and (prime(M-j)=1) then writeln(M,'=',j,'+',M-j);

j:=j+2;

if j< M div 2 then goto m2;

readln; end.

2.24.

, 2, 3 5. 10 1, 2, 3, 4, 5, 6, 8, 9, 10 12. . , 1000 .

1 ()

, ( ), , 2, 3 5. 1, . . , . ( ).

. , 2, 3 5. . , 1, -

. 2, . 2 , 3 5 . 3. 2 3 , 5 . , , "" , .

2 ()

kl, k2, k3 , 2, 3 5. , . . .

2_24.bas ( )

DEFLNG A-Z

DIM Xam(1000)

CLS

Xam(0)=l

PRINT 1,1

FOR J=l TO 999

x2=Xam(k2)*2

x3=Xam(k3)*3

x5=Xam(k5)*5

IF x2<=x3 AND x2<=x5 THEN Xam(J)=x2: k2=k2+l

IF x3<=x2 AND x3<=x5 THEN Xam(J)=x3: k3=k3+l

IF x5<=x2 AND x5<=x3 THEN Xam(J)=x5: k5=k5+l

PRINT USING "###:########## "; J,Xam(J) ;

REM

IF J MOD 20=0 THEN INPUT A$

NEXT J

END

2_24. ( )

#include <stdio.h>

#include <conio.h>

main() (

long Xam[1000], 2, , 5;

int j;

int k2=0,k3=0,k5=0;

clrscr(); -

Xam[0]-l,-

printf("%4d %9d",l,l);

for(i=i; j<1000; j++) {

x2=Xam[k2]*2; x3=Xam[k3]*3;

x5=Xam[k5]*5;

if(x2<=x3 && x2<=x5)

{Xam[j]=x2; k2++;}

if(x3<=x2 && x3<=x5)

{Xam[j]=x3; k3++;}

if(x5<=x2 && x5<=x3)

{Xam[j]=x5; k5++;}

printf("\n%4d %91d",j,Xam[j]);

if(j % 20==0)

getch(); }

getch (); }

2_24.pas ( )

program Hamming;

uses crt;

var

Xam:array[i..1000] of longint;

x2,x3,x5 : longint;

j:integer; const

k2:integer=l;

k3:integer=l;

k5:integer=l; begin

Xam[l]:=1;

writeln(l:4,' ',1:10);

for j:=2 to 1000 do

begin

x2:=Xam[k2]*2;

x3:=Xam[k3]*3;

x5:=Xam[k5]*5;

if(x2<=x3) and (x2<=x5) then

begin

Xam [ j ] : =x2;

inc(k2);

end;

if(x3<=x2) and (x3<=x5) then

begin Xam[j] := x3; inc(k3);

end;

if (x5 <= x2) and (x5 <= x3) then begin Xam[j] := x5; inc(k5);

end;

writeln(j:4,' ',Xam[j]:10);

if (j mod 20)=0 then readln; end; readln; end.

2_24a.pas ( )

program Hammingl;

uses crt,WinDos;

var

j,Hour,Hourl,min,mini,sec,seel,hsec,hsecl : word; i,k : longint;

begin clrscr;

gettime(Hour,min,sec,hsec); i:=l; j:=0; repeat k:=i;

while (k mod 2)=0 do k:=k div 2;

while (k mod 3)=0 do k:=k div 3;

while (k mod 5)=0 do k:=k div 5;

if k=l then begin

{ write(i:10); } { } inc(j);

end; inc(i);

until j>=1000;

gettime (Hourl,min1,secl,hsecl) ;

writeln;

writeln(Hour,':',min,':',sec,'.',hsec);

writeln(Hourl,':',minl,':',seel,'.',hsecl); readln; end.

2.25.

m/n, "" . :

26/65 = 2/5.

, ( < ) , . 10/20, 10/30, ... , 20/30, ... , 80/90, ... .

1 ()

, 10/11 98/99, . , . , .

2_25.bas

R CLS

R 10/11 98/99

FOR N=11 TO 99 FOR M=10 TO N-1

T=M/N : '

Nlo=N MOD 10 : '

Nhi=(N-Nlo)\10 : '

Mlo=M MOD 10 : '

Mhi=(M-Mlo)\10 : '

IF Mlo=0 THEN GOTO 100

""

IF Nlo=Mlo AND ABS(T-Mhi/Nhi)<.001 THEN PRINT M;"/";N;"=";Mhi;"/";Nhi

END IF

IF Nlo=Mhi AND ABS(T-Mlo/Nhi)<.001 THEN

PRINT M;"/";N;"=";Mlo;"/";Nhi

END IF

IF Nlo<>0 THEN

IF Nhi=Mlo AND ABS(T-Mhi/Nlo)<.001 THEN

PRINT M;"/";N;"=";Mhi;"/";Nlo

END IF

IF Nhi=Mhi AND ABS(T-M1o/Nlo)<.001 THEN

PRINT M;"/";N;"=";Mlo;"/";Nlo

END IF

END IF

100 :

NEXT M NEXT N END

2_25.

/* */

#include <stdio.h>

#include <conio.h>

#include <math.h>

main() !

char n,m,n_lo,n_hi,m_lo,m_hi; float t; clrscr();

/* 10/11 98/99 */ for(n=ll; n<100; ++) for(m=10; m<n; m++) {

t=(float)m/n; /* */

n_lo=n % 10; /* */

n__hi=n/10; /* */

m_lo=m % 10; /* */

m_hi=m/10; /* */

if(m % 10 == 0) continue;

/* "" */

i£(n lo==m lo && n_hi!=0 && fabs (t-m_hi/n_hi) <1.e-3)

printf("\n%d/%d=ld/id",m,n,m_hi,n_hi);

if(n_lo==m_hi && n_hi!=0 && fabs(t-m_lo/n_hi)<le-3)

printf ("\n%d/%d=%d/%d",m,n,m_lo,n__ni) ;

i£(n hi==m_lo ££ n__lo!=0 ££ fabs(t-m_hi/n_lo)<le-3)

printf("\n%d/%d=ld/ld",m,n,m_hi,n_lo);

if(n_hi==m_hi && n_lo!=0 && fabs(t-m_lo/n_lo)<le-3)

printf("\n%d/%d=%d/%d",m,n,m_lo,n_lo); }

getch{); }

2_25.pas

program drobi;

{ }

uses crt;

var

n, m, n_lo, n__hi, m_lo, m_hi: byte ; t:real; begin

clrscr;

{ 10/11 98/99 } for n:=ll to 99 do for m:=10 to n-1 do begin

t:=m/n; { }

n_lo:=n mod 10; { }

n_hi:=n div 10; { }

m_lo:=m mod 10; { }

m_hi:=m div 10; { }

if m_lo=0 then continue;

{ "" }

if (n_lo=m_lo) and (n_hi<>0) and (abs(t-m_hi/n_hi)<le-3) then

writeln(m:3, '/',n:2, ' = ',m_hi:l, '/',n_hi) ;

if (n_lo=m_hi) and (n_hi<>0) and (abs(t-m_lo/n_hi)<le-3) then

writeln(m:3,'/',n:2,'=',m_1:1,'/',njii);

if (n_hi=m_lo) and (n_lo<>0) and (abs(t-m_hi/n_lo)<le-3) then

writeln(m:3,'/',n:2,'=',m_hi:l,'/',n_lo);

if (n_hi=m_hi) and (n__lo<>0) and (abs(t-m_lo/n_lo)<le-3) then

writeln(m:3,'/',n:2,'=',m_lo:1,'/',n_lo); end; readln;

end.

2.26.

, , , N (N = 2 + 2 + 2 + d2). , N . : 4 = 22 = 12 + 12 + 12 + 12. , , , , , - .

1 ()

, . , , , :

a<b<c<d< sqrt(N).

2 ()

N2 , N 1, 2. , , N , , . N , , , . , . PROBA 5 , 4 ( = 1) ( = 0) , , d.

2_26.bas

R

DECLARE SUB PROBA (Al AS INTEGER, Bl AS INTEGER,Cl AS INTEGER, Dl AS INTEGER,N AS LONG)

INPUT " - ", N& IF (N& MOD 2)=0 THEN

REM N -

PROBA 0,0,1,1,N&

PROBA 0, 0,0,0,N&

PROBA 1,1,1,1,N& ELBE

REM N -

PROBA 0,0,0,1,N&

PROBA 0,1,1,1,N& END IF END

SUB PROBA (Al AS INTEGER,Bl AS INTEGER,Cl AS INTEGER,Dl AS INTEGER,N AS LONG)

'

Q=INT(SQR{N)+.5) : '

FOR A%=A1 TO Q STEP 2: S1=A%*A%

FOR B%=B1 TO Q STEP 2: S2=S1+B%*B%

FOR C%=C1 TO Q STEP 2: S3=S2+C%*C%

FOR D%=D1 TO Q STEP 2

IF S3+D%*D%=N THEN

PRINT " :"

PRINT "a=";A%,"b=";B%,"c=";C%,"d=";D% END

END IF

NEXT D%: NEXT C%: NEXT B%: NEXT A% END SUB

2_26.

/* */

#include <math.h>

#include <stdlib.h>

#include <conio.h>

void proba(int al,int bl,int cl,int dl,long N) ;

main() {

long N;

printf("\n - ");

scanf("%ld",&N);

if((N % 2)==0) /* N - */

{

proba(0,0,l,l,N);

proba(0,0,0,0,N);

proba(1,1,1,1,N) ;

}

else /* N - */

{ proba(0,0,0,l,N);

proba(0,l,l,l,N); } }

void proba(int al,int bl,int cl,int dl,long N)

/* */

{ int a,b,c,d,q; long sl,s2,s3;

q=floor(sqrt(N)+.5);

/* */

for(a=al; a<=q; a+=2)

{

sl=a*a;

for(b=bl; b<=q; b+=2)

{

s2=sl+b*b;

for(c=cl; c<=q; c+=2)

{

s3=s2+c*c;

for(d=dl; d<=q; d+=2)

if (s3+d*d=N)

{

printf("\n :");

printf("\n a=%d b=%d c=%d d=%d",a,b,c,d);

getch();

exit(0);

}

}

}

}

return;

}

2_26.pas

program razlojenie;

{ }

var

N:longint;

procedure proba(al,bl,cl,d1:integer; N:longint);

{ }

var

a,b,,d,q:integer; s1,s2,s3:longint;

begin

q:=round(sqrt(N)); { }

a:=al;

while a<=q do begin

sl:-a*a,- b:-b1;

while b<=q do begin s2:=sl+b*b;

c:=cl;

while c<=q do begin s3:=s2+c*c;

d:=dl; while d<=q do begin

if(s3+d*d=N) then begin

writeln(' :');

writeln('=',,' b=',b,' c=',c,' d=',d);

readln;

exit;

end;

inc(d,2);

end; inc(c,2);

end;

inc(b,2);

end; inc(a,2);

end;

end;

begin

write(' - ');

readln(N);

if odd(N) then begin { N - }

proba(0,0,0,l,N);

proba(0,1,1,1,N);

end else begin { N - }

proba (0, 0,1,1,N) ;

proba(0,0,0,0,N);

proba(1,1,1,1,N);

end;

end.

2.27.

, , . : ( ), , - .

1 ()

(*+*+=) . , , . A*+B*+ , .

, , , , - .

2_27.bas

R

DECLARE FUNCTION R!(U!,V!,Ul!,VI!,U2!,V2!)

PRINT " "

INPUT X1,Y1,X2,Y2,X3,Y3

'XI = 0: Yl = 0: X2 = 0: Y2 = 1: X3 = 1: Y3 = 0

PRINT " " INPUT X, Y

IF (R(X,Y,X1,Y1,X2,Y2)*R(X3,Y3,XI,Yl,X2,Y2)>0 AND R(X,Y,X2,Y2,X3,Y3)*R(X1,Y1,X2,Y2,X3,Y3)>0

AND R(X,Y,X1,Y1,X3,Y3)*R(X2,Y2,X1,Y1,X3 Y3)>0) THEN

PRINT " ": END

END IF

IF (R(X,Y,X1,Y1,X2,Y2)*R(X3,Y3,X1,Y1,X2,Y2)<0

OR R(X,Y,X2,Y2,X3,Y3)*R(X1,Y1,X2,Y2,X3,Y3)<0

OR R(X,Y,X1,Y1,X3,Y3) *R(X2,Y2,X1.,Y1,X3,Y3)<0) THEN

PRINT " ": END

END IF

PRINT " " END

FUNCTION R(U,V,U1,V1,U2,V2)

REM (U,V) ,

R (U1,V1) (U2,V2)

R=0

IF ((U-U1)*(V2-V1)-(V-V1)*(U2-U1)>0) THENR=1

IF ((U-U1)*(V2-V1)-(V-V1)*(U2-U1)<0) THENR=-1

END FUNCTION

2_27.c

/* */

#inclucte <stdio.h>

#include <iconic. h>

int r(float u,float v,float ul,float vl, float u2,float v2);

main () {

float X0,y0,xl,yl,x2,y2,x3,y3;

float k012,k012, k023, k123, K312, k213;

printf("\n \");

scanf("%f %f %f %f %f %f",&xl,&yl,&x2,&y2,&x3,&y3);

printf{"\n \n");

scanf("%f %f",&x0,&y0);

k012=r(x0,y0,xl,yl,x2,y2);

k312=r(x3,y3,xl,yl,x2,y2);

k023=r(x0,y0,x2,y2,x3,y3);

k123=r(xl,yl,x2,y2,x3,y3);

k013=r(x0,y0,xl,yl,x3,y3);

k213=r(x2,y2,xl,yl,x3,y3);

if(k012*k312>0 && k023*k!23>0 SS k013*k213>0)

{

puts(" ");

getch();

exit(0);

} if(k012*k312<0 || k023*k123<0 || k013*k213<0)

{

puts(" "); getch(); exit(O);

}

puts(" "); getch();

}

/*--------------------------------------*/

int r(float u,float v,float ul,float vl, float u2,float v2)

/* (U,V) ,

(U1,V1) (U2,V2) */

{

float s=(u-ul)*(v2-vl)-(v-vl)*(u2-ul);

if(s>0) return 1;

if(s<0) return -1;

return 0;

}

2_27.pas

program triangle;

( }

var

Xd,y0,xl,yl,x2,y2,x3,y3:real;

k012, k312,k023,k!23,k013,k213:integer;

function r(u, v,ul, vl,u2, v2:real) :integer;

{ (U,V) ,

(U1,V1) (U2,V2) }

var

s:real;

begin

s: = (u-ul) * (v2-vl)- (v-vl) * (u2-ul) ;

r:=0;

if s>0 then r:=l;

if s<0 then r:=-l;

end;

begin

writeln(' ');' readln(xl,yl,2,2,,);

writeln(' ');

readln(x,);

k012:=r(x0,y0,xl,yl,x2,y2);

k312:=r(x3,y3,xl,yl,x2,y2);

k023:=r(0,0,2,2,,);

k123:=r(xl,yl,x2,y2,x3,y3);

k013:=r(x0,y0,xl,yl,x3,y3);

k213:=r(2,2,!,!,,);

if (k012*k312>0) and (k023*k!23>0) and (k013*k213>0) then begin

write(' ');

halt;

end;

if (k012*k312<0) or (k023*k!23<0) or (k013*k213<0) then begin

write (' '),' halt; end;

write (' ' ) ;

readln;

end.

2.28.

, . 50 100 . . 1 9 ( 1 ). , .

1 ()

N+1, . . > N+1, Ml = k* (N+1), . . N+I. , . . 0. , : q, N+1-q. N+1 . , , 1 , , .

2_28.bas

R

DEFINT A-Z

ml:

CLS

RANDOMIZE 32767

N = INT(RND * 25) + 26

PRINT " - "; N; " 1 5"

m2:

PRINT " . ? - "

m4:

a$ = INKEY$: IF a$ = "" THEN GOTO m4

k = ASC(a$) - 48

IF 1 > k OR k > 5 THEN PRINT " !": GOTO m2

N = N - k

PRINT " "; N

IF N = THEN PRINT "! !": GOTO ex

IF (N MOD 6) = 0 THEN k = 1 ELSE k = N MOD 6

N = N - k

PRINT " "; k; " "; N

IF N = 0 THEN

PRINT " , !": GOTO ex

ELSE

GOTO m2

END IF ex: .,

PRINT " ? - (y/n) : "

m3:

a$ = INKEY$: IF a$ = "" THEN GOTO m3

IF a$ = "y" THEN GOTO ml

END

2_28.

//

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main() { int N,k,j; ml:

clrscr();

randomize();

N=random(25)+26;

printf("Ha - %d. 1 5.\n",N);

m2:

printf("\n . ? - ");

k=getche()-48;

if(l>k || k>5) {printf("\n !"); goto m2;}

N=N-k;

printf("\n %d",N);

if(N==0){printf("\n ! !");

goto ex;}

k=(N%6=0)?l:N%6;

printf ("\n %d %d",k,N);

if(N==0){printf("\n K , !");

goto ex;

}

goto m2;

ex:

printf(" ? - (y/n) : ");

if(getch()=='y') goto ml; )

2_28.pas

{ }

program game_mun;

uses Crt;

var

N,k,j:integer; label

ml,m2,ex;

begin ml:

clrscr;

randomize;

N:=random(25)+26;

writeln('Ha - ',N,'. 1 5.');

m2:

writeln('Bain . ? - ');

k:=Ord(ReadKey)-48;

if(l>k) or (k>5) then

begin

writeln('TaK !1); goto m2; end;

N:=N-k;

writeln(' ',N);

if N=0 then

begin writeln('! !'}; goto ex; end;

if (N mod 6)=0 then k:=l else k:=N mod 6;

N:=N-k;

writeln(' ',k,' ',N);

if N=0 then

begin writeln(' , !'); goto ex; end;

goto m2; ex:

writeln(' ? - (y/n) : ');

if ReadKey ='y' then goto ml; end.

2.292.31.

, . " " (Ulam S. .). , . , , , .

, , (. 2.1).

. 2.1.

-. , . , , (x=x+1) (=+1), (x=x-1) (y=y-l), , . . N .

. , . , , (-,), (2*n)2.

, (n,-n+l), (2*n-1)2. N q, , "" N. q -- , qz-N . (-q,q) . q , , N, , . (q, -q+1).

(,) . (-q, q) (q,-q+i) . , .

, = . , , > . (,) , , , . :

:

1 : > 0, > 0

2 : < 0, > 0, > ||

3 : < 0, > 0, < ||

4 : < 0, < 0

, (0,0) N =(2*0)2. , , (, ).

, . , b, , d. .

,

coord_to_n.

2_29.bas

DECiARF, FUNCTION CoordToN! (&, Y&)

REM

INPUT " : ", &, Y&

& = CoordToN(X&, Y&)

PRINT " = "; &

END

FUNCTION CoordToN (XS, Y&)

IF X& <= Y& THEN '

IF & >= 0 AND Y& > 0 THEN GOTO m12 ' 1

IF & < 0 AND Y& >= 0 AND Y& >= ABS(X&) THEN ' 2

' 1 2

m12:

0 = -Y&: 0 = Y&: N& = (2 * 0)^2'

CoordToN = N& + 0 - &: EXIT FUNCTION

ELSE

' 3 4

0 = &: 0 = -XS: N& = (2 * 0)^2 CoordToN = N& + 0 - Y&:

EXIT FUNCTION

END IF

END IF '

IF X& <= 0 AND Y& < 0 THEN GOTO mab'

IF x& >= 0 AND'YS < 0 AND ABS<Y&) >= & THEN ' 2, 3

mab: 0 = -Y& +1: Y0 = -Y&: N& = (2 * - 1) ^2

CoordToN = N& - + &: EXIT

FUNCTION ELSE

' , d

0 = &: 0 = -& + 1: N& = (2 * 0 - 1) ^ 2

CoordToN = N& + YS - 0

END IF

END FUNCTION

2_29.

#include <stdio.h>

#include <conio.h>

#include <math.h>

long sqr(long x);

long coord_to_n(long x,long y);

void main() { long x,y,M;

printf("\n : ");

scanf("%ld %ld",Sx,&y);

M=coord to n(x,y); .

printf("\nEro = %ld",M);

getch(); }

long coord_to_n(long x,long y) { long N,x0,y0;

if(x<=y) //

{

if(>=0 && >0) goto m12; // 1

if(x<0 && y>=0 && y>=labs(x)) // 2 {// 1 2

ml2:

0=-;

0=;

N=sqr(2*); //

return N+x0-x; } // 3 4

0=; 0=-; N=sqr(2*0); return N+y0-y; }

//

if(<=0 && <0) goto mab; // 2

if(x>=0 && <0 && labs(y)>=x) // 3

{// 1, 2

mab:

x0=-y+l;

y0=-y;

N=sqr(2*0-1);

return N-x0+x;

} // 3, 4

0=;

0=-+1;

N=sqr(2*x0-l);

return N+y-y0;

long sqr(long x)
{ return x*x; }

2_29.pas

program CoordToN;
var

M,x,y:longint;

function coord_to_n(x,y:longint}:longint;
var

x0,y0,N:longint;
label ml2,mab;
begin

if x<=y then { }
begin if (x>=0) and(y>0) then goto m!2; { 1 }
if (x<0) and (y>=0) and (y>=abs(x)) then { 2 }
begin { 1 2 }
m12: x0:=-y; y0:=y; N:=sqr(2*x0); { }

coord__to_n: = N+xO-x; exit;
end;
{ 4 }

x0:=x; y0:=-x; N:=sqr(2*x0);
coord_to_n:=N+yO-y; exit;
end;
{ }

if(x<=0) and (y<0) then goto mab; { }
if(x>=0) and (y<0) and (abs(y)>=x) then { b }
begin { a, b }
mab: x0:=-y+l; y0:=-y; N:=sqr(2*x0-l);

coord_to_n:= N-xO+x; exit;
end;
{ , d }

x0:=x; y0:=-x+l; N:=sqr(2*xO-l);
coord_to_n:= N+y-y0;
end;

begin

writeln(' : ');

readln(x,);

.=toord_to_a(x,y);

writeln (' = \);
readln;
end.

n_to_coord coord_to_N NI N2, N . .

2_30.bas

DECLARE SUB NtoCoord (N&, X&, YS)

REM

INPUT " : ", N&

NtoCoord N&, xl&, yl&

INPUT " : ", N&

NtoCoord N&, 2&, 2&

dx = xl& - x2&: dy = yl& - 2&

PRINT " = ", SQR(dx * dx + dy * dy)
END

SUB NtoCoord (N&, X&, Y&)
DIM k AS LONG, j AS LONG, d AS LONG
j = INT(SQR(N&) + .5)
d = N& - j * j
k = j \ 2

PRINT "j="; j, "d="; d, "k="; k
IF (j MOD 2) THEN
IF d > 0 THEN

X& = k + 1: Y& = -k + d
ELSE

X& = k + 1 + d: Y& = -k
END IF
ELSE

IF d > 0 THEN

X& = -k: Y& = k - d
ELSE

X& = -k - d: Y& = k
END IF
END IF
END SUB

2_30.

//
#include <stdio.h>
#include <conio.h>

#include <math.h>

void n_to_coord(long N, long *x, long *y) ;

void main()
{ long N,xl,yl,x2,y2;
double dx,dy;

printf("\n : ");
scanf("%ld",&N);
n_to_coord(N,Sxl, &yl);

printf("\n : ");
scanf("%ld",&N);
n_to_coord(N,&x2,&y2) ;
dx=xl-x2; dy=yl-y2;

printf(" = %f",sqrt(dx*dx + dy*dy));
getch();
}

void n_to_coord(long N,long *x,long *y)
( long k,j,d;

j=floor(sqrt(N)+0.5);
d=N-j*j; k=j / 2;
if (j % 2)

{ if (d>0) { *x=k+l; *y=-k+d; }

else { *x=k+l+d;*y=-k; }
}
else

{ if (d>0) { *x=-k; *y=k-d; }

else { *x=-k-d; *y=k; )
}
}

2_30.pas

program spiral;
var

xl, yl, x2,y2,N:longint;

dx, dy: double;
procedure n_to_coord(N:longint;var x,y:longint);

var

k,j,d:longint;
begin
}

j:=trunc(sqrt(NJ+0.5) ;

d:=N-sqr(j);

k:=j div 2; if odd(j) then begin

if d>0 then begin x:=k+l; y:=-k+d;end else begin x:=k+l+d; y:=-k; end; end

else if d>0 then begin x:=-k; y:=k-d; end else begin x:=-k-d; y:=k; end; end; begin

writeln(' '); readln(N);

n_to_coord(N,xl,yl); writeln(' '); readln(N);

n_to_coord(N,x2, 2); dx:=xl-x2; dy:=yl-y2;

writeln(' = ',sqrt(dx*dx + dy*dy)); readln; end.

, , (. 2.2).

. 2.2.

- . 0, 1, 2, ... "" , . , k k+1 , :

X + = b(k) .

, , . , . . 2.1.

2.1. ,




nDiag

nBeg

nEnd

(g, g)

(xEnd, yEnd)

0

0

0

(0,0)

(0,0)

1

1

2

(1,0)

(0,1)

2

3

5

(0,2)

(2,0)

3

6

9

(3,0)

(0,3)

4

10

14

(0,4)

(4,0)

5

15

20

(5,0)

(0,5)

6

21

27

(0,6)

(6,0)

7

28

35

(7,0)

(0,7)

8

36

44

(0,8)

(8,0)

9

45

54

(9,0)

(0,9)

10

55

65

(0,10)

(10,0)

. 2.1 :

nEnd = nBeg + nDiag nBeg(nDiag-t-l) = nEnd(nDiag) + 1

nDiag -- , (0, nDiag) , . (noiag, 0) , .

, . .

dx dy , ( 0) ( ) ( ). , , nextpoint. , .

2_31.bas

DECLARE SUB NEXTPOINT (X&, Y&)

DIM nDiag AS LONG '

DIM xBeg AS LONG, yBeg AS LONG '

DIM nPoints AS LONG '

DIM nBeg AS LONG '

DIM dx(8), dy(S)

DATA -1,-1,-1,0,1,1/ 1/ 0

FOR J = 0 TO 7: READ dx(J): NEXT J

DATA -1, 0, 1,1,1,0,-1,-1

FOR J = 0 TO 7: READ dy(J): NEXT J

ml:

CLS

INPUT " : ", N

nBeg = 0: = N: = 0: g = 0

FOR k = 0 200000000

= - k

IF < 0 THEN EXIT FOR

nBeg = nBeg + k NEXT k

nDiag = k - 1: nPoints = k

IF (nDiag MOD 2) = 0 THEN '

yBeg = nDiag xN = xBeg + (N - nBeg) yN = yBeg - (N - nBeg)

ELSE '

xBeg = nDiag

xN = xBeg - (N - nBeg)
yN = yBeg + (N - nBeg) END IF
PRINT " : "

FOR k = 0 7
X& = xN + dx(k)
Y& = yN + dy(k)
NEXTPOINT X&, Y& NEXT k PRINT
PRINT " - (y/n) : ";

m2:
A$ = INKEY$: IF A$ = "" THEN GOTO m2

IF A$ = "y" THEN GOTO m1 END
SUB NEXTPOINT (X&, Y&)
SHARED nDiag AS LONG, xBeg AS LONG, yBeg AS LONG, nPoints AS LONG
DIM N AS LONG, J AS INTEGER
IF X& < 0 OR Y& < 0 THEN EXIT SUB
nBeg = 0: nDiag = X& + Y&: xBeg = yBeg = 0
nPoints = nDiag + 1
FOR J = 0 TO nPoints - 1: nBeg = nBeg + J: NEXT J
IF (nDiag MOD 2) = 0 THEN
yBeg = nDiag: N = nBeg + yBeg - Y&
ELSE
xBeg = nDiag: N = nBeg + xBeg - X&
END IF
PRINT N; ",";

END SUB

2_31.


#include <stdio.h>
#include <conio.h>
void nextpoint(long x,long y) ;
int dx[8]={-l,-l,-l,0,l,l, 1, 0};
int dy[8]={-l, 0, 1,1,1,0,-!,-!};
long nDiag, //
xBeg,yBeg, // nPoints, // nBeg; //
void main() { long N,M,
xN,yN, // x,y,k; clrscr (); m:
printf("\n\n : "} ; scanf("%ld",&N);
// nBeg=0; M=N;
==0;
for(k=0;;k++)
{ M=M-k;
if(M<0) break; nBeg=nBeg+k; }
nDiag=k-l; nPoints=k;
if((nDiag % 2)=0) // { yBeg= nDiag; xN=xBeg+(N-nBeg); yN=yBeg-(N-nBeg);
)
else //
{ xBeg=nDiag;
xN=xBeg-(N-nBeg); yN=yBeg+(N-nBeg); }
printf(" :\n" ); for(k=0;k<8;k++) { x = xN + dx[k]; = yN + dy [k] ; nextpoint(x,y); }
printf(" - (y/n) : ");
if(getch(}==''} goto m; }
void nextpoint(long x,long y) { long N;
if(x<0||y<0) return;
nBeg=0;
nDiag=x+y;
xBeg=yBeg=0;
nPoints=nDiag+l;
for fint j=0;j<nPoints;j++) nBeg+=j;
if((nDiag % 2)==0) { yBeg=nDiag; N=nBeg+yBeg-y;}
else { xBeg=nDiag; N=nBeg+xBeg-x; }

printf("%ld ",N);

}


2_31.pas


program spirall;

uses Crt;

label ml;

var
nDiag, { }
xBeg,yBeg, ( }
nPoints, { }
nBeg, { }
N,M,
xN,yN, { }
,,k: longint; const
dx : array [0..7] of integer =(-1,-1,-1,0,1,1, 1, 0);
dy : array [0..7] of integer =(-1, 0, 1,1,1,0,-1,-1);
procedure nextpoint(x,y:longint);

var
N: longint ,-
j. integer/" begin
if (x>=0)and(y>=0) then
bey in nBeg:=0; nDiag;=x+y; xseg:=0;
yBeg:=0;
nPoints:=nDiag+l;
for j:=0 to nPoints-1 do nBeg:=nBeg+j;

if ((nDiag mod 2)=0) then begin
yBeg:=nDiag; N:=nBeg+yBeg-y; end
else begin
xBeg:=nDiag; N:=nBeg+xBeg-x; end;
write(N,', ') ; end; end; begin
clrscr; ml:
write(' : ');

readln(N);
{ }

nBeg:=0;

M:=N;

xBeg:=0;

yBeg:=0;

k:=0;

repeat
M:=M-k;
if M<0 then break; nBeg:---nBeg+k;

k:=k+l;

until k<0; nDiag:=k-l;

nPoints:=k;
if(nDiag mod 2)=0 then { } begin
yBeg:=nDiag;

xN:=xBeg+(N-nBeg) ;

yN:=yBeg-(N-nBeg) ; end
else { }
begin
xBeg:=nDiag; xN:=xBeg-(N-nBeg) ;

yN:=yBeg+(N-nBeg) ; end;
writeln(' : ');

for k:=0 to 7 do begin
x := xN + dx[k]; := yN + dy[k]; nextpoint(x,y);

end;
writeln;

writeln(' - (y/n) : ');

if ReadKey='y' then goto ml;

end.

      . - 3GL   - 4GL   5GL  

, - - , , , , , .




 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 -> - _.
Bourabai Research -  XXI Bourabai Research Institution