C语言 求1000到9999以内所有回文数的和 5
有2种方法1:数字反向后与原数字相同即是回文数。方法2:数字转成字符串,左右对称即是回文数。
#include<stdio.h>
#include<stdlib.h>
int ishw1(int num);
int ishw2(int num);
int main()
{
int i;
printf("方法一,数字倒置后比较两个数字是否相等,回文数有:\n");
for(i=1000;i<9999;i++)
if(ishw1(i))printf("%d ",i);
printf("\n");
printf("方法二,数字转成字符串首尾循环比较是否对称,回文数有:\n");
for(i=1000;i<9999;i++)
if(ishw2(i))printf("%d ",i);
printf("\n");
return 0;
}
int ishw1(int num)
{
int num2=0,num1=num;
while(num1/10>0)
num2=num2*10+num1%10,num1=num1/10;
num2=num2*10+num1;
if(num==num2) return 1;
else return 0;
}
int ishw2(int num)
{
char snum[5],*p0=NULL,*p1=NULL;
itoa(num,snum,10);
snum[4]=0;
p0=p1=snum;
while(*p1!=0)p1++;
p1--;
while(*p0==*p1 && p0<=p1)p0++,p1--;
if(p0>p1)return 1;
else return 0;
}
clear
n=0
for x=1000 to 9999
a=int(x/1000)
b=int((x-a*1000)/100)
c=int((x-a*1000-b*100)/10)
d=mod(x,10)
if a=d and b=c and mod(x,6)=0
n=n+1
endif
endfor
? n
set talk on
return