/**
* 1.解题思路:从1-9枚举,判断是否存在N。通过to_string把乘积转成字符串,再通过substr取末尾子串比较即可
*
* 2.参考博客:https://blog.csdn.net/liuchuo/article/details/84972987
**/
#include <iostream>
#include <string>
using namespace std;
int main() {
int m;
cin >> m;
while (m--) {
int k, flag = 0;
cin >> k;
//从1-9枚举
for (int n = 1; n < 10; n++) {
int mul = n * k * k;
//将数字常量转换为字符串
string smul = to_string(mul), sk = to_string(k);
//substr:主要功能是复制子字符串,要求从指定位置开始,则子字符串将延续到源字符串的结尾
string smulend = smul.substr(smul.length() - sk.length());
if (smulend == sk) {
printf("%d %d\n", n, mul);
flag = 1;
break;
}
}
if (flag == 0) printf("No\n");
}
return 0;
}
1091 N-自守数
最新推荐文章于 2022-10-20 22:43:47 发布