c语言问题~~~回文数!!急,拜托高人指点!!

2025-02-25 01:22:17
推荐回答(3个)
回答1:

添加上满足你补充要求的C程序:
#include

int palindrome(unsigned int x)
{
unsigned int y;
unsigned int s=0;
y=x;
while(y>0)
{
s=s*10+y%10;
y=y/10;
}
if(s==x)
return 1;
else
return 0;
}

void main()
{
printf("符合条件的N为:\n");
for(int n=1;n<=200;n++)
{
if(palindrome(n*n))
{
printf(" %-5d\n",n);
}
}
}
运行结果如下:

符合条件的N为:
1
2
3
11
22
26
101
111
121
Press any key to continue

已经上机验证过了!!!

#include
#include
#include

int palindrome(unsigned int x) //
{
unsigned int y;
unsigned int s=0;
y=x;
while(y>0)
{
s=s*10+y%10;
y=y/10;
}
if(s==x)
return 1;
else
return 0;
}

void Reverse(unsigned char *buf,int size)
{
int i;
unsigned char temp;
if (buf==NULL || size<2) return;
for (i=0;i {
temp=buf[i];
buf[i]=buf[size-i-1];
buf[size-i-1]=temp;
}
}

void main()
{
int count=0;
unsigned int x,temp;
char strt[10];
cout<<"输入一个十进制整数:";
cin>>x;
if(palindrome(x)&&x<2147483647)
{
cout<<"算出的回文数为:"< return;
}
int i=1,itmp;//
while(x<2147483647)//2147483647)
{
i=1;
itmp=pow(10,i);
while(x/itmp)
{
i++;
itmp=pow(10,i);
}
itoa(x,strt,10);
Reverse((unsigned char*)strt,i);
temp=atoi(strt);
x+=temp;
if(palindrome(x))
{
cout<<"算出的回文数为:"< return;
}
}
cout<<"没有算出回文数!\n";
}

回答2:

#include

main()
{
int i,j,a,b,s=0,n=0,s0;
b=0;
scanf("%d",&s);
s0 = s;
do
{
s=s/10;
n++;
}
while (s>0);
s = s0;
for (i=1;i<=n;i++)
{
a=s%10;
s=s/10;
b=b+a;
}
printf("%d\n",b);
}

回答3:

#define MAX 2147483647 #include main() {long int n,m,re(); int count=0; printf("please enter a number optionaly:"); scanf("%ld",&n); printf("The generation process of palindrome:\n"); while(!nonre((m=re(n))+n)) { if(m+n>=MAX) { printf(" input error, break.\n"); break; } else {printf(" [%d]: %ld+%ld=%ld\n",++count,n,m,m+n); n+=m; } } printf(" [%d]: %ld+%ld=%ld\n",++count,n,m,m+n); printf("Here we reached the aim at last !\n");} long re(a) long int a;{ long int t; for(t=0;a>0;a/=10) t=t*10+a%10; return(t);} nonre(s) long int s; {if(re(s)==s) return(1); else return(0); }应该是OK的,是标准答案