梦见摔碗 碎了:计算进编程问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/11 18:09:49
定义一个函数,判断数X是否为回文数,如果是则返回1,否则返回0,在主函数中调用该函数,求1~10000的回文数的个数

没有完全按要求,但这个是对的
因为我不知道0 ,1 ,2, 3, 4,5,6,7,8,9这样的数是不是也属于回文数,按定义应该是的,对不?

#include"stdio.h"
#include"math.h"
int hui(int x)
{int a[5],k=5,f=0,t=x;
int i,j=0;
for(i=1;i<=5;i++)
{a[i-1]=t/pow(10,5-i);
t=t-pow(10,5-i)*a[i-1];}
while(a[j]==0) j++;
k=k-j;
for(j=0;j<k;j++)
f=10*f+a[4-j];
if(x==f) return 1;
else return 0;
}
main()
{int x,i=0;
for(x=0;x<=10000;x++)
{if(hui(x))
{ i++;
printf("%6d",x);
if(i%10==0) printf("\n");}
}
printf(" \n you %d ge hui wen shu\n ",i);
}

program GetCount;
uses
SysUtils;
var
I,Count: Integer;
function IsPaldo(ANum: Integer): Byte;
var
I,Len: Integer;
begin
Result := 1;
Len := Length(IntToStr(ANum));
for I := 1 to Trunc(Len/2) do
if IntToStr(ANum)[I] <> IntToStr(ANum)[Len - I + 1]then
begin
Result := 0;
Break;
end;
end;
begin
Count := 0;
for I := 1 to 10000 do
if IsPaldo(I) = 1 then
Inc(Count);
Writeln('The Count Of The Pal Between 1 To 10000 Is: ', Count);
Readln(I);
end.