c++自守数
C++代码实现自守数的判断:
#include <iostream>
#include <cmath>
using namespace std;
bool isAutomorphic(int num) {
int square = num * num;
while (num > 0) {
if (num % 10 != square % 10) {
return false;
}
num /= 10;
square /= 10;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isAutomorphic(num)) {
cout << num << " is an automorphic number." << endl;
} else {
cout << num << " is not an automorphic number." << endl;
}
return 0;
}
自守数(Automorphic Number)是指一个数的平方的尾数等于该数自身的数。例如,5的平方是25,是以5结尾的,所以5是一个自守数
原文地址: http://www.cveoy.top/t/topic/iDMV 著作权归作者所有。请勿转载和采集!