C++ ①暴力 ②令a=b,当b<=x且b^2>x时,符合条件(不知道不满足时其他也都不满足怎么证明)
#include <iostream> using namespace std; int main() { int x; cin >> x; bool f=false; for (int b=1; b<=x; b++) { for (int a=b; a<=x; a+=b) { if (a*b>x && a/b<x) { cout << a << ' ' << b; f=true; break; } } if (f) break; } if (!f) cout << -1; } // 64 位输出请用 printf("%lld")
② b = a * n 即 a = b / n;a * b = b^2 / x > n;a/b = 1/n < x 即 1/x < n;令 n = 1,则只需满足 b^2 > x 即可。
#include <iostream> using namespace std; int main() { int x; cin >> x; int b=1; while (b<=x) { if (b*b>x) break; b++; } if (b<=x && b*b>x) cout << b << ' ' << b; else cout << -1; } // 64 位输出请用 printf("%lld")