题解 | #自守数#
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
将数的比较,转化为字符串的比较
#include <iostream> int main(int argc, char const *argv[]) { int n = 0; while (std::cin >> n) { int count = 2; for (int i = 2; i <= n; i++) { long j = i * i; std::string sl = std::to_string(j); std::string si = std::to_string(i); std::size_t sn = sl.rfind(si); //从长串的末尾开始找到子串的位置 if (sl.size() - sn == si.size()) //长串的长度-子串首字符的位置=子串的长度 { count++; } } std::cout << count << std::endl; } return 0; }