TÌM SỐ ĐỐI XỨNG Ta biết rằng một số gọi là số đối xứng nếu đọc từ trái qua phải hay từ phải qua trái của biểu diễn số đó ở dạng thập phân đều cho cùng một số. Ví dụ: a = 123321 là một số đối xứng. Yêu cầu: Cho số b, hãy tìm số a là số đối xứng nhỏ nhất lớn hơn b. Dữ liệu: Vào từ file văn bản PALIN.INP gồm một dòng duy nhất ghi số ? (? ≤ $10^{100000}$) Kết quả: ghi ra file văn bản PALIN.OUT gồm một dòng duy nhất ghi số ?. Ví dụ: PALIN.INP PALIN.OUT 99912 99999

2 câu trả lời

const fi='palin.inp';
      fo='palin.out';
      max=round(1e6);
type bignum=array [1..max] of longint;
var a,b: bignum;
    s:ansistring;
    t,i,dem:longint;
function check(a:bignum):boolean;
var c,t:longint;
begin
        t:=length(s);
        for c:=1 to t div 2 do
                        if a[1+c-1]<>a[t-c+1] then exit(false);
                exit(true);
end;
begin
        assign(input,fi);
        reset(input);
        read(s);
        close(input);
        fillchar(a,sizeof(a),0);
        fillchar(b,sizeof(b),0);
        dem:=0;
        t:=length(s);
        b[t]:=1;
        for i:=1 to t do
              a[i]:= ord(s[i])-48;
        while not check(a) do
        begin
                for i:=t downto 1 do
                begin
                     a[i]:=(a[i]+b[i]) mod 10;
                     if a[i]=10 then inc(a[i-1]);
                end;
        end;
        assign(output,fo);
        rewrite(output);
        for i:=1 to t do
                write(a[i]);
        close(output);
end.

program tim_so_doi_xung

Uses crt;
var st: string;
function KT (xau: string): boolean;
var i, dem: integer;
begin
dem:=0;
for i:= 1 to length(st) div 2 do
if xau<>xau[length(xau)-i+1] then inc(dem);
if dem<>0 then KT:=False else KT:=True;
end;
begin
clrscr;
write ('Nhap so co do dai khong qua 100 chu so: '); readln (st);
if length(st)>100 then
begin
write ('Nhap khong hop le! An Enter va chay lai!');
readln; exit;
end;
if KT(st)=True then write ('Day la so doi xung!') else write ('Day la so khong doi xung!');
readln
end.

Câu hỏi trong lớp Xem thêm