用C++编写程序求10~2000之间所有的回文数,判断一个数是否为回文数。要用函数fun(int n)来实现。

如题所述

long re(int a)
{
long int t;
for(t=0;a>0;a/=10)//将整数反序
t=t*10+a%10;
return t;
}
int nonre(lint n)//判断给定的整数是否为回文数
{
if(n>10 && n<2000)
{
if(re(n)==n)
return 1;//是返回1
else
return 0;//不是返回0
}
return 0;//不是返回0
}
void main()
{
int n,m;
int count=0;
printf("please input a number optionaly:");
scanf("%ld",&n);
printf("The genetation process of palindrome:\n");
while(!nonre((m=re(n))+n))//判断整数与其反序相加后是否为回文数
{
printf("[%d]:%ld+%ld=%ld\n",++count,n,m,m+n);
n+=m;//累加
}
printf("[%d]:%d+%ld=%ld\n",++count,n,m+n);
printf("Here we reached the aim at last.\n");//输出最好得到的回文数
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-05
#include<iostream>
using namespace std;
int main()
{
bool fun(int );
cout<<"10~2000之间所有的回文数:"<<endl;
for(int i =11;i<2000;i++)
{
if(fun(i))
cout<<i<<" ";

}
cout<<endl;
int num;
cout<<"请输入一个10~2000之间的数"<<endl;
cin>>num;
if(fun(num))
cout<<num<<"是回文数"<<endl;
else
cout<<num<<"不是回文数"<<endl;
return 0;
}
bool fun(int n)
{
int a[4]={0};
a[0] = n/1000;//千位
a[1] = (n/100)%10;//百位
a[2] = (n%100)/10;//十位
a[3] = n%10;//个位
if(a[0])
{
if((a[0] ==a[3])&&(a[1] ==a[2]))
return 1;
else
return 0;

}
else if((a[0]==0)&&a[1])
{
if(a[1]==a[3])
return 1;
else
return 0;

}
else if((a[0]==0)&&(a[1]==0)&&a[2])
{
if(a[2]==a[3])
return 1;
else
return 0;

}
else
return 0;

}本回答被网友采纳
第2个回答  2012-03-29
回文数 是什么数呀?
相似回答