模拟/usaco 1.2.4 Palindromic Squares

题意

  给定一个进制B,输出所有大于等于1小于等于300且它的平方用B进制表示时是回文数的数

分析

  模拟,写清楚进制转换和判断回文即可

Accepted Code

 1 {

 2 ID: jessiel2;

 3 PROG: palsquare

 4 LANG: PASCAL

 5 }

 6 Program palsquare;

 7 Const

 8   Infile = 'palsquare.in';

 9   Outfile = 'palsquare.out';

10 Var

11   now,old:Ansistring;

12   b,i:LOngint;

13   z:Array[10..35]Of Char;

14 Function change(n:Longint):ansiString;

15 Var

16   a:Array[0..100000]Of Longint;

17   temp:ansistring;

18   k,i:Longint;

19 Begin

20   k:=0;

21   While n<>0 Do Begin

22     Inc(k);

23     a[k]:=n Mod b;

24     n:=n Div b;

25   End;

26   temp:='';

27   For i:=k Downto 1 Do

28     If a[i]>9 Then temp:=temp+z[a[i]] Else temp:=temp+chr(a[i]+48);

29   Exit(temp);

30 End;

31 

32 Function check(k:ansiString):Boolean;

33 Var

34   len,i:Longint;

35   k1:ansiString;

36 Begin

37   len:=length(k);

38   k1:='';

39   For i:=len Downto 1 Do k1:=k1+k[i];

40   For i:=1 To len Do

41     If k[i]<>k1[i] THen Exit(false);

42   Exit(true);

43 End;

44 

45 Begin

46   Assign(input,infile);Reset(input);

47   Assign(output,outfile);Rewrite(output);

48   ReadLn(b);

49   For i:=1 To 26 Do z[i+9]:=chr(i+64);

50   For i:=1 To 300 Do Begin

51     now:=change(i*i);

52     If check(now) Then Begin

53       old:=change(i);

54       WriteLn(old,' ',now);

55     End;

56   End;

57   Close(input);Close(output);

58 End.

 

你可能感兴趣的:(USACO)