帮忙看看结果对不对?

labant 2003-04-06 11:37:00
题目:求11-999中,原数,平方,立方都回文数的数(回文数如:121,696,24942)
怎么只有一个数:11。是不是错了,哪错了请指出!!
#include <stdio.h>

int js(int n)
{ int i,strl,half;char ch[20];
itoa(n,ch,10);
strl=strlen(ch);
half=strl/2;
for(i=0;i<half;i++)
if(ch[i]!=ch[--strl])break;
if(i>=half)return 1;
else return 0;


}

main()
{ long m;
FILE *out;

out=fopen("out.dat","w");
for(m=11;m<1000;m++)
{
if(js(m)&&js(m*m)&&js(m*m*m))
{printf("m=%4ld,m*m=%6ld,m*m*m=%8ld\n",m,m*m,m*m*m);
fprintf(out,"m=%4ld,m*m=%6ld,m*m*m=%8ld\n",m,m*m,m*m*m);}
}
fclose(out);
}
...全文
30 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
我估计数应该是
0
1
2
11
101
111
1001
10001
10101
11011
100001
101101
110011
1000001
1001001
1100011
1101011
...
如果你有时间多算上一会儿的话,就不用再穷举了
我看是这么一个规律

10以上的数,两头都是1
中间对称,而且中间不能够完全为1
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
a.c

#include <stdio.h>

int main( void );

int
main( void )
{
int i, j, t, l;
char t1[ 30 ];
char t2[ 30 ];
for( i = 0; i < 1000000; i++ )
{
t = 0;

j = 0;
sprintf( t1, "%d", i );
memset( t2, 0, 30 );
l = strlen( t1 ) - 1;
while( t1[ j ] )
{
t2[ l - j ] = t1[ j ];
j++;
}
if( !strcmp( t1, t2 ) )
t++;

j = 0;
sprintf( t1, "%d", i * i );
memset( t2, 0, 30 );
l = strlen( t1 ) - 1;
while( t1[ j ] )
{
t2[ l - j ] = t1[ j ];
j++;
}
if( !strcmp( t1, t2 ) )
t++;

j = 0;
sprintf( t1, "%d", i * i * i );
memset( t2, 0, 30 );
l = strlen( t1 ) - 1;
while( t1[ j ] )
{
t2[ l - j ] = t1[ j ];
j++;
}
if( !strcmp( t1, t2 ) )
t++;

if( t == 3 )
fprintf( stdout, "%d %d %d\n", i, i * i, i * i * i );
}
return 0;
}

makefile
CC=gcc

all:a
a:a.o
$(CC) $^ -o $@
a.o:a.c
$(CC) -c $<
clean:
rm -f a.o a

运行结果
~
[root@linux venus]# ./a
0 0 0
1 1 1
2 4 8
11 121 1331
101 10201 1030301
111 12321 1367631
1001 1002001 1003003001
[root@linux venus]#
很有规律吧
用户 昵称 2003-04-07
  • 打赏
  • 举报
回复
include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

/* 计算x的y次方 */
unsigned long pow(unsigned long x, unsigned long y) {
unsigned long i, j = 1;
if (y == 0) {
return 1;
}
for ( i = 0; i < y ; i++) {
j *= x;
}
return j;
}
/* 求整数的长度 */
unsigned long num_len (unsigned long num) {
unsigned long len;

len = 1;
while(1){
if ( (num = (unsigned long)(num / 10)) != 0) {
len ++;
}else{
break;
}
}
return len;
}

/* 判断是否符合要求 */
short is_num_good(unsigned long num) {
unsigned long len, i, j;

if (num == 0 || num == 1){
return TRUE;
}

len = num_len(num);

for ( i = 0; i <= (unsigned long)len/2; i++) {
if ( (unsigned long)((num % (unsigned long)pow(10,i+1) ) / pow(10 , i))
!= ((unsigned long)(num/(unsigned long)pow(10,(len-i-1)))%10)){
return FALSE;
}
}
return TRUE;
}
/* 得到反向的数 */
unsigned long reverse_num(unsigned long num) {
unsigned long len, i, j;

if (num == 0 || num == 1){
return num;
}

len = num_len(num);

j = 0;
for ( i = 0; i <len; i++) {
j = j *10 + ((num%(unsigned long)pow(10,(i+1)) / pow(10 , i)) );
}
return j;
}
main()
{
unsigned long num_start, num_reverse, count = 0;

scanf("%d", &num_start);
printf("num_start is %d \n", num_start);
while(1) {
if (is_num_good(num_start) == TRUE) {
break;
}
count ++;
num_reverse = reverse_num(num_start);
printf("%d: reverse num:from %d to %d\n", count,num_start, num_reverse);
num_start += num_reverse;
}
printf("last number is %d, total %d times.\n",
num_start, count);
}
这是完全用整数来实现的,还可以只用字符串,那样不受数值大小的限制。

lymgf 2003-04-07
  • 打赏
  • 举报
回复
没错,不过建议这样写:

int js(int n)
{
int i,l;
char ch[20];
itoa(n,ch,10);
l=strlen(ch)-1;
i=0;
while(i<l)
{
if(ch[i]!=ch[l])
break;
i++;
l--;
}
if(i>=l)
return 1;
return 0;
}
webmelon 2003-04-07
  • 打赏
  • 举报
回复
在程序中加上
#include <stdlib.h>
#include <string>
结果是:
m= 11,m*m= 121,m*m*m= 1331
m= 101,m*m= 10201,m*m*m= 1030301
m= 111,m*m= 12321,m*m*m= 1367631
没有错啊!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧