您的位置:首页 > 大数据 > 人工智能

UVa 10976 Fractions Again?!(分数拆分)

Ramay7 2015-12-30 21:58 87 查看
题目链接:UVa 10976

题意:

输入一个正整数k,找到所有的正整数x>y,使得1/k=1/x+1/y。

CODE:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int x[10010], y[10010], k, cnt;

int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif 
	while (cin >> k)
	{
		cnt = 0;
		for (int i = k+1; i <= 2 * k; i++)
		{
			if (k*i % (i - k)) continue;
			y[cnt] = i;
			x[cnt++] = k*i / (i - k);
		}
		cout << cnt << endl;
		for (int i = 0; i < cnt; i++)
			printf("1/%d = 1/%d + 1/%d\n", k, x[i], y[i]);
	}
	return 0;
}
标签: