C++编程:找出在(10,1000)内的数m使得m,m*m,m*m*m都是回文数,我的程序不知道哪里有问题,能帮我看看吗
#include<iostream>usingnamespacestd;inthuiwen(inta);intmain(){cout<<"求m属于(10,1000)使得m...
#include<iostream>using namespace std;int huiwen(int a);int main(){ cout << "求m属于(10,1000)使得m,m^2,m^3都是回文数" << endl; int m; int huiwen(int a); for (m = 11; m < 1000; m++) { if ((huiwen(m) == 1) && (huiwen(m*m) == 1) && (huiwen(m*m*m) == 1)) cout << m << endl; else continue; } return 0;}int huiwen(int a){ int b=a,c=a,len=1,i=0,res; while ((b/ 10) != 0) { len++; b= b / 10; } int *p = new int[len]; while (c != 0) { p[i] = c % 10; c = c / 10; i++; } for (int x = 0; x < len / 2; x++) { if (p[i] != p[len - 1 - x]) res = 0; break; } if (res != 0) return 1; delete[]p;}
展开
展开全部
第8行可以去掉,第35行p[i]改成p[x]
delete[] p 记得写在return前面,不然是没用的
我也写了一个回文判断,仅供参考:
int huiwen(int data)
{
int reverse = 0;
int temp = data;
while (temp)
{
int c = temp % 10;
reverse = reverse * 10 + c;
temp /= 10;
}
return data == reverse;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询