c++:输出所有10-1000内自身、平方、立方均为回文数的整数,我写的程序输出了11-21,想请教下问题在哪?

c++:输出所有10-1000内自身、平方、立方均为回文数的整数,我写的程序输出了11-21,想请教下问题在哪?

问题描述:

#include
using namespace std;
extern int n=10, i, j,m=0;
int hui(int n)
{
i = 0;
j = n;
while (j != 0)
{
i = i * 10 + j % 10;
j = j / 10;
}
if (i == n)
m++;
else
m = m;
return 0;
}
int main()
{
while (n <= 1000)
{
hui(n);
hui(n*n);
hui(n*n*n);
if (m == 3)
{
cout << n << "自身、平方、立方均为回文数";
}
n++;
}
return 0;
}

首先11没问题;
然后,m==3,n==12~21,while循环中m仍在3的基础上累加,但是都累加0,m一直等于3,就一直输出;
直到22,又累加了3次,m==6,以后m都不等于3了,就都不输出了。

解决办法:在while循环尾部加一个m=0;

如果有帮助,希望采纳,谢谢

如果问题得到解决,请点我回答左上角的采纳和向上的箭头,谢谢

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

/*
//gcc的话需加上这段,vc++不需要
void strreverse(char* begin, char* end) {
    char aux;
    while(end>begin)
        aux=*end, *end--=*begin, *begin++=aux;
}

void itoa(int value, char* str, int base) {
    static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    char* wstr=str;
    int sign;

       // Validate base
    if (base<2 || base>35){ *wstr='\0'; return; }

        // Take care of sign
    if ((sign=value) < 0) value = -value;

    // Conversion. Number is reversed.
    do {
              *wstr++ = num[value%base];
        } while(value/=base);
    if(sign<0) *wstr++='-';
    *wstr='\0';

    // Reverse string
    strreverse(str,wstr-1);
}
//*/

int hui(int n)
{
char buf[15];
itoa(n, buf, 10);
for (int i = 0; i < strlen(buf); i++)
{
    if (buf[i] != buf[strlen(buf) - 1 - i])
        return 0;
}
return 1;
}
int main()
{
int n=10, i, j,m=0;
while (n <= 1000)
{
m = 0;
m+=hui(n);
m+=hui(n*n);
m+=hui(n*n*n);
if (m == 3)
{
cout << n << "自身、平方、立方均为回文数\n";
}
n++;
}
return 0;
}