Pascal
Modified: Wednesday, 22-12-2021 07:00 AM
Số xoay là số khi xoay nó 180° thì nó vẫn không thay đổi.
Yêu cầu:
Nhập giá trị N từ File Xoay.INP
Ghi các số xoay có N chữ số ra File Xoay.OUT
Ví dụ:
Input: 2
Output: 11 69 88 96
Code bên dưới chạy đúng nhưng chưa tối ưu. Hy vọng sẽ nhận được code tối ưu hơn từ mọi người.
var ANum : array[1..4] of char;
n:byte; fo:text;
procedure InputANum;
begin
ANum[1] := '1';
ANum[2] := '6';
ANum[3] := '8';
ANum[4] := '9';
end;
procedure NhapN;
var fi:text;
begin
assign(fi,'D:/Xoay.INP');
reset(fi);
readln(fi,n);
close(fi);
end;
procedure Xuat(s:string);
var i:byte;
st:string;
begin
st := '';
for i:=1 to length(s) do
begin
if (s[i]='1') or (s[i]='8') then
st := s[i]+st
else if s[i]='6' then
st := '9'+st
else if s[i]='9' then
st := '6'+st
end;
if n mod 2 <>0 then
begin
writeln(fo,s+'1'+st);
writeln(fo,s+'8'+st);
end
else
writeln(fo,s+st);
end;
procedure Xuli(x:byte);
var i1,i2,i3,i4,i5,i6,i7:byte;
s:string;
begin
assign(fo,'D:/Xoay.OUT');
rewrite(fo);
for i1:=1 to 4 do
begin
if x=1 then
begin
s := ANum[i1];
Xuat(s);
end
else for i2:=1 to 4 do
begin
if x=2 then
begin
s := ANum[i1]+ANum[i2];
Xuat(s);
end
else for i3:=1 to 4 do
begin
if x=3 then
begin
s := ANum[i1]+ANum[i2]+ANum[i3];
Xuat(s);
end
else for i4:=1 to 4 do
begin
if x=4 then
begin
s := ANum[i1]+ANum[i2]+ANum[i3]+ANum[i4];
Xuat(s);
end
else for i5:=1 to 4 do
begin
if x=5 then
begin
s := ANum[i1]+ANum[i2]+ANum[i3]+ANum[i4]+ANum[i5];
Xuat(s);
end
else for i6:=1 to 4 do
begin
if x=6 then
begin
s := ANum[i1]+ANum[i2]+ANum[i3]+ANum[i4]+ANum[i5]+ ANum[i6];
Xuat(s);
end
else for i7:=1 to 4 do
begin
if x=7 then
begin
s := ANum[i1]+ANum[i2]+ANum[i3]+ANum[i4]+ANum[i5]+ANum[i6]+ANum[i7];
Xuat(s);
end;
end;
end;
end;
end;
end;
end;
end;
close(fo);
//readln;
end;
begin
InputANum;
NhapN;
//n := 4;
Xuli(n div 2);
end.