如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”。例如 3×922=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守数。
本题就请你编写程序判断一个给定的数字是否关于某个 N 是 N-自守数。
输入格式:
输入在第一行中给出正整数 M(≤20),随后一行给出 M 个待检测的、不超过 1000 的正整数。
输出格式:
对每个需要检测的数字,如果它是 N-自守数就在一行中输出最小的 N 和 NK2 的值,以一个空格隔开;否则输出 No
。注意题目保证 N<10。
输入样例:
3
92 5 233
输出样例:
3 25392
1 25
No
#include<iostream> using namespace std; bool isEqual(int n,int x){ while(n != 0 && x != 0){ if(x % 10 != n % 10) return false; x /= 10; n /= 10; } return true; } int main(){ int m; cin >> m; while(m--){ int n; cin >> n; int temp = n*n; bool flag = true; for(int i = 1; i < 10; i++){ int ans = i * temp; if(isEqual(ans,n)){ cout << i << " " << ans << endl; flag = false; break; } } if(flag) cout << "No" << endl; } return 0; }