Codeforces 1294C Product of Three Numbers

题目链接:

Codeforces 1294C Product of Three Numbers

思路:

此题就是求一个数的约数:一个数n的约数可以从2遍历到sqrt(n),最后它本身也是一个约数;
此题就是求出两个这样的约数,第三个数就是原数除以前两个约数的结果;注意三个数都需要大于1且互不相同;

代码:

#include<bits/stdc++.h>

using namespace std;

inline int find(int x, int st) {
	if(x < 2) return -1;
	int sq = sqrt(x) + 1;
	for(int i = st; i <= sq; i++) {
		if(x % i == 0) return i;	
	}
	return x >= st ? x : -1;
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int a, b, c;
		a = find(n, 2);
		if(a == -1) { cout << "NO\n"; continue; }
		n /= a;
		b = find(n, a + 1);
		if(b == -1) { cout << "NO\n"; continue; }
		n /= b;
		if(n == a || n == b || n <= 2) { cout << "NO\n"; continue; }
		cout << "YES\n" << a << ' ' << b << ' ' << n << '\n';
	}
	return 0;
}
发布了299 篇原创文章 · 获赞 8 · 访问量 7433

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104076659
今日推荐