Bài tập hệ cơ số (16-12-2022)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Program So_abcd;

uses crt;
procedure xuly;
var a,b,c,d:byte;
begin
for a:=1 to 9 do
for b:=0 to 9 do
for c:=0 to 9 do
for d:=0 to 9 do
if (sqr((a*10+b)+(c*10+d))=(a*1000+b*100+c*10+d)) then
writeln(a*1000+b*100+c*10+d);
end;
Begin
clrscr;
xuly;
readln
End.

Program Sn_vtk;
uses crt;
var n:longint;
k:byte;
procedure nhap(var n:longint; var k:byte);
begin
write('N = ');readln(n);
write('k = ');readln(k);
end;
procedure xuly(n:longint; k:byte);
var s:string;
begin
str(n,s);
if ((k=0) or (k>length(s))) then
write('Ket qua tim duoc la: -1')
else
write('Ket qua tim duoc la: ',s[k]);
end;
Begin
clrscr;
nhap(n,k);
xuly(n,k);
readln
End.

Program So_bt;
uses crt;
var n1,n2:integer;
procedure nhap(var n1,n2:integer);
begin
write('Nhap n1: ');readln(n1);
write('Nhap n2: ');readln(n2);
end;
function kt(n:integer):boolean;
var t:byte;
begin
if (n<10) then
exit(false);
t:=n mod 10;
n:=n div 10;
while (n>0) do
begin
if (n mod 10>=t) then
exit(false);
t:=n mod 10;
n:=n div 10;
end;
exit(true);
end;
procedure xuly(n1,n2:integer);
var i:integer;
begin
writeln('Cac so bac thang tu ',n1,' den ',n2,' la:');
for i:=n1 to n2 do
if (kt(i)) then
write(i,' ');
end;
Begin
clrscr;
nhap(n1,n2);
xuly(n1,n2);
readln
End.

Program Cs10_cs;
uses crt;
var n:integer;
x:byte;
procedure nhap(var n:integer; var x:byte);
begin
write('Nhap so can doi: ');readln(n);
write('Nhap co so: ');readln(x);
end;
function cs2(n:integer):string;
var t:string;
begin
cs2:='';
while (n>0) do
begin
str(n mod 2,t);
cs2:=t+cs2;
n:=n div 2;
end;
end;
function cs16(n:integer):string;
var t:string;
begin
cs16:='';
while (n>0) do
if (n mod 16>=10) then
begin
case (n mod 16) of
10:cs16:='A'+cs16;
11:cs16:='B'+cs16;
12:cs16:='C'+cs16;
13:cs16:='D'+cs16;
14:cs16:='E'+cs16;
15:cs16:='F'+cs16;
end;
n:=n div 16;
end
else
begin
str(n mod 16,t);
cs16:=t+cs16;
n:=n div 16;
end;
end;
procedure xuly(n:integer; x:byte);
begin
case x of
2:write(cs2(n));
16:write(cs16(n));
end;
end;
Begin
clrscr;
nhap(n,x);
xuly(n,x);
readln
End.

Program Cs_cs10;
uses crt;
var s:string;
x:byte;
procedure nhap(var s:string; var x:byte);
begin
write('Nhap co so: ');readln(x);
write('Nhap xau can doi: ');readln(s);
end;
function somu(n,k:byte):integer;
begin
somu:=1;
while (k>0) do
begin
somu:=somu*n;
dec(k);
end;
end;
function cs2(s:string):integer;
var t,i,j:byte;
begin
cs2:=0;
j:=0;
for i:=length(s) downto 1 do
begin
val(s[i],t);
cs2:=cs2+(t*somu(2,j));
inc(j);
end;
end;
function cs16(s:string):integer;
var t,i,j:byte;
begin
cs16:=0;
j:=0;
for i:=length(s) downto 1 do
if (s[i] in ['A'..'F']) then
begin
case s[i] of
'A':t:=10;
'B':t:=11;
'C':t:=12;
'D':t:=13;
'E':t:=14;
'F':t:=15;
end;
cs16:=cs16+(t*somu(16,j));
inc(j);
end
else
begin
val(s[i],t);
cs16:=cs16+(t*somu(16,j));
inc(j);
end;
end;
procedure xuly(s:string; x:byte);
begin
case x of
2:write(cs2(s));
16:write(cs16(s));
end;
end;
Begin
clrscr;
nhap(s,x);
xuly(s,x);
readln
End.

Program Do_ben;
var n:longint;
procedure doctep;
var fi:text;
begin
assign(fi,'Persist.inp');reset(fi);
read(fi,n);
close(fi);
end;
function tich(n:longint):longint;
begin
if (n=0) then
tich:=0
else
tich:=1;
while (n>0) do
begin
tich:=tich*(n mod 10);
n:=n div 10;
end;
end;
procedure xuly;
var fo:text;
doben,max,dem:byte;
t,i:longint;
begin
assign(fo,'Persist.out');rewrite(fo);
max:=0;
for i:=1 to n do
begin
dem:=0;
t:=i;
while (t>9) do
begin
dem:=dem+1;
t:=tich(t);
end;
if (dem>=max) then
begin
max:=dem;
doben:=i;
end;
end;
write(fo,doben);
close(fo);
end;
Begin
doctep;
xuly;
End.

You might also like