Uses Crt;
Const E=[0,1,2,3,4,10,11,12,13,20,21,22,30,40];
Function hg(n,x:integer):shortint;
begin
hg:=(x div n)-10*(x div (10*n));
end;
Function dk(x:integer):boolean;
begin
dk:=false;
if (hg(1,x)<>hg(10,x))and(hg(1,x)<>hg(100,x))and(hg(1,x)<>hg(1000,x))and(hg(10,x)<>hg(100,x))
and(hg(10,x)<>hg(1000,x))and(hg(100,x)<>hg(1000,x))and(x<9877)and(x>122)
then dk:=true;
end;
Function ss(x,y:shortint):shortint;
begin
ss:=0;if x=y then ss:=1;
end;
Function diem(x,y:integer):shortint;
begin
diem:=10*(ss(hg(1000,x),hg(1000,y))+ss(hg(100,x),hg(100,y))+ss(hg(10,x),hg(10,y))
+ss(hg(1,x),hg(1,y)))+ss(hg(1000,x),hg(100,y))+ss(hg(1000,x),hg(10,y))+ss(hg(1000,x),hg(1,y))
+ss(hg(100,x),hg(1000,y))+ss(hg(100,x),hg(10,y))+ss(hg(100,x),hg(1,y))+ss(hg(10,x),hg(1000,y))
+ss(hg(10,x),hg(100,y))+ss(hg(10,x),hg(1,y))+ss(hg(1,x),hg(1000,y))
+ss(hg(1,x),hg(100,y))+ss(hg(1,x),hg(10,y));
end;
PROCEDURE May;
VAR x:array[-5..7]of integer;
chdx:array[-5..7]of shortint;i:shortint;
zz: longint;
BEGIN
clrscr; readln;
writeln('********************************************************************************');
writeln(' LUU Y: Ban phai tra loi that chinh xac qua moi lan may tinh doan.');
writeln('De tra loi chinh xac ban chi can nhap vao mot so co 2 chu so');
writeln('Vi du: "21" co nghia la 2A 1B, "40" co nghia la may da doan dung!');
writeln; writeln;
x[-5]:=-1111;x[-4]:=-1111;x[-3]:=-1111;x[-2]:=-1111;x[-1]:=-1111;x[0]:=-1111;
chdx[-5]:=0;chdx[-4]:=0;chdx[-3]:=0;chdx[-2]:=0;chdx[-1]:=0;chdx[0]:=0;
i:=1;
while i<9 do
begin
zz:=1;
randomize;
repeat
x[i]:=random(10000);
zz:=zz+1;
if (zz=20000)or(i=8) then
begin
gotoxy(3,17); Writeln('Ban da cho diem sai o lan nao do phai khong?');
readln;
exit;
end;
until dk(x[i])
and(x[i]<>x[i-1])and(x[i]<>x[i-2])and(x[i]<>x[i-3])and(x[i]<>x[i-4])and(x[i]<>x[i-5])and(x[i]<>x[i-6])
and(diem(x[i-6],x[i])=chdx[i-6])and(diem(x[i-5],x[i])=chdx[i-5])and(diem(x[i-4],x[i])=chdx[i-4])
and(diem(x[i-3],x[i])=chdx[i-3])and(diem(x[i-2],x[i])=chdx[i-2])and(diem(x[i-1],x[i])=chdx[i-1]);
if x[i]<1000 then write(' Lan thu ',i,' 0',x[i],' ');
if x[i]>1000 then write(' Lan thu ',i,' ',x[i],' ');
zz:=1;
repeat
gotoxy(21,7+i);clreol;read(chdx[i]);
until chdx[i] in E;
if chdx[i]=40 then exit;
i:=i+1;
end;
END;
PROCEDURE Ban;
VAR i:shortint;y:integer;
x:array[1..7]of integer;
BEGIN
clrscr;randomize;
repeat
y:=random(10000);
until dk(y);readln;
writeln('********************************************************************************');
writeln(' Moi ban thu tai suy luan !!!');
writeln;
i:=1;
while i<=7 do
begin
repeat
gotoxy(1,6+i);clreol;
write(' Lan doan thu ',i,': ');gotoxy(25,6+i);read(x[i]);
until dk(x[i]);
if diem(x[i],y)=40 then
begin
if y<1000 then
begin
writeln(' Ban doan dung roi,chinh la so: 0',y);
exit;
end;
if y>1000 then
begin
writeln(' Ban doan dung roi,chinh la so: ',y);
exit;
end;
end;
gotoxy(32,6+i);writeln(diem(x[i],y)div 10,'A',' ',diem(x[i],y)-10*(diem(x[i],y)div 10),'B');
i:=i+1;
end;
if y<1000 then writeln(' Da het luot doan, so can tim la: 0',y);
if y>1000 then writeln(' Da het luot doan, so can tim la: ',y);
END;
PROCEDURE Ketthuc;
BEGIN
clrscr;gotoxy(34,13);write('Chao tam biet!');
gotoxy(24,15);write('@ Written by: Hoàng Xuân Thanh - 2002');readln;
halt;
END;
{Chuong trinh chinh}
VAR ch,tr:char;
BEGIN
Repeat
clrscr;
writeln(' TRO CHOI DOAN SO GIUA BAN VA MAY TINH');writeln;
writeln(' Cho mot so co 4 chu so khac nhau. Ban phai tim ra so nay qua 7 lan doan.');
writeln('Sau moi lan doan may se cho ban mot thong tin dang xA yB. Voi x la so chu so ');
writeln('ban doan dung va nam dung vi tri con y la so chu so dung nhung sai vi tri');writeln;
writeln(' AN 1 HOAC 2 DE LUA CHON'); writeln;
repeat gotoxy(0,9);clreol;
write(' 1. Ban la nguoi doan so 2. May tinh se doan so cua ban ');
read(tr)until tr in['1','2'];if (tr='1') then Ban; if (tr='2') then May;
writeln('*******************************************************************************');
gotoxy(3,19);write(' Ban co choi tiep khong?(c/k) ');
repeat gotoxy(34,19);clreol;read(ch)until ch in['c','C','k','K'];readln;
if ch in['c','C']then continue;
if ch in['k','K']then Ketthuc;
Until false;
END.
Đây là nguyên đoạn code của cái DoanSo ở đề bài!
Function diem(x,y) Đánh giá sự khác biệt giữa hai số x và y theo giả thiết.