程序如下:
#include "stdio.h"
int main()
{
int u,m
int sum=0
printf("请输入要判断的数u=")
scanf("%d",&u)
m=u
while(m)
{
sum=sum*10+m%10
m=m/10
}
if(sum==u)
printf ("u是亮扰回文脊陵数")
else
printf("u不是回文数")
printf("\n")
return 0
}
程序运行结果:
假设输入一樱键戚回文数12321
publicclassPlalindrome{publicstaticvoidmain(String[]args){System.out.println(11is+(isPlalindrome(11)?:not)+Plalindromenumber)System.out.println(123is+(isPlalindrome(123)?:not)+Plalindromenumber)System.out.println(17251is+(isPlalindrome(17251)?:not)+Plalindromenumber)System.out.println(2882is+(isPlalindrome(2882)?:not)+Plalindromenumber)}publicstaticbooleanisPlalindrome(intnumber){//此方法实现判断数字是不是回文数Stringnum=String.valueOf(number)returnnewStringBuffer(num).reverse().toString().equalsIgnoreCase(num)}}---------------
11 is Plalindrome number
123 is not Plalindrome number
17251 is not Plalindrome number
2882 is Plalindrome number for i = 100 to 99999 '这里从100开始 后面可以随便填,我这里填99999 表示所有3位数到五位数之间的回文数
if StrReverse(i)=i then print i '用StrReverse函数 判断倒序后的数和原来数是否相同,如果相同者表示此数为颂谨回文数
next #include<stdio.h>intx,yseparate(int*data,intn){ inti,j y=0 while(n!=0) { *(data+y)=n%10n=n/10y++ } *(data+y)='\0' for(i=0,j=y-1i<=ji++,j--) { if(*(data+i)!=*(data+j)){ printf(%d不是回文!!!\n,x)break } } if(i ==y-1) printf(是回文数)}voidmain(){inta[99]printf(请输入一个正整数:)scanf(%d,&x)separate(a,x)}另外一种实现方法(c++)更简便
#include<iostream>
using namespace std
bool symm(long m)
{
long temp = m,n=0
while (temp)
{
n = n*10+temp%10
temp = temp/10
}
return (m == n)
}
int main(int argc, _TCHAR* argv[])
{
long m
cout<<请输入一个整数:
cin>>m
cout<<输入了<<symm(m)<<个回文数!
return 0
} #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#defineINF99999999usingnamespacestdconstintMAX=110000+10chars[MAX*2]intp[MAX*2]intmain(){ while(scanf(%s,s)!=EOF){ intlen=strlen(s),id=0,maxlen=0 for(inti=leni>=0--i){//插入'#' s[i+i+2]=s[i] s[i+i+1]='#' }//插入了len+1个'#',最终野旁基的s长度是1~len+len+1即2*len+1,首启吵尾s[0]和s[2*len+2]要插入不同的字符 s[0]='*'//s[0]='*',s[len+len+2]='\0',防止在while时p[i]越界 for(inti=2i<2*len+1++i){ if(p[id]+id>i)p[i]=min(p[2*id-i],p[id]+id-i) elsep[i]=1 while(s[i-p[i]]==s[i+p[i]])++p[i] if(id+p[id]<i+p[i])id=i if(maxlen<p[i])maxlen=p[i] } cout<<maxlen-1<<endl } return0}
思路:以12321为例,mod 10,取余数为1,赋值给S;再信迹整除10,得1232,mod10, 取余数2,连接S得扰拿S=12……执行一半长度个循环,最后S为后半滑李并段数字的逆序数,再与前半段比较,如果一致即为回文数,代码如下:Private Sub Command1_Click()
nub = Val(Text1.Text)
k = Len(nub) \ 2
For i = 0 To k - 1
t = nub / 10 ^ i Mod 10
s = s &t
Next i
If Val(s) = Val(Left(nub, k)) Then
Print nub &"是回文数"
Else
Print nub &"不是回文数"
End If
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)