USACO Section 2.1 Ordered Fractions

/*
ID: lucien23
PROG: frac1
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct Fraction
{
	int numerator;
	int denominator;
	Fraction(){}
	Fraction(int x, int y)
	{
		numerator = x;
		denominator = y;
	}
} Fraction;

bool isPrimes(int x, int y);

bool compFrac(Fraction frac1, Fraction frac2)
{
	if (frac1.numerator/(double)frac1.denominator < frac2.numerator/(double)frac2.denominator)
	{
		return true;
	}
	return false;
}

int main()
{
	ifstream infile("frac1.in");
	ofstream outfile("frac1.out");
	if(!infile || !outfile)
	{
		cout << "file operation failure!" << endl;
		return -1;
	}

	int N;
	infile >> N;

	vector<Fraction> fractions;
	fractions.push_back(Fraction(0, 1));
	fractions.push_back(Fraction(1, 1));

	for (int i=1; i<=N; i++)
	{
		for (int j=1; j<i; j++)
		{
			if (isPrimes(j, i))
			{
				fractions.push_back(Fraction(j, i));
			}
		}
	}

	sort(fractions.begin(), fractions.end(), compFrac);

	for (vector<Fraction>::iterator it=fractions.begin(); it!=fractions.end(); it++)
	{
		outfile << it->numerator << "/" <<it->denominator << endl;
	}

	return 0;
}

/*
 *推断两数是否互质
 */
bool isPrimes(int x, int y)
{
	for (int i=2; i<=x; i++)
	{
		if (x%i == 0 && y%i == 0)
		{
			return false;
		}
	}

	return true;
}
时间: 2024-10-19 02:36:42

USACO Section 2.1 Ordered Fractions的相关文章

【USACO 2.1】Ordered Fractions

/* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚举,约分,排序,去重 */ #include<cstdio> #include<algorithm> using namespace std; struct node{ int nu,deno; double v; }a[40000]; int n,cnt; int cmp(node

USACO 2.1 Ordered Fractions

Ordered Fractions Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between

洛谷P1458 顺序的分数 Ordered Fractions

P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解为: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 给定一个自然数N,1<=n&

洛谷 P1458 顺序的分数 Ordered Fractions

P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解为: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 给定一个自然数N,1<=n<=160,请编程按分数值递增的顺序输出所有解. 注:①0和任意自然数的最大公约数就是那个自然数②互质指最大公约数等于

USACO Section 2.1 Healthy Holsteins

/* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #include <vector> using namespace std; bool compFun(int x, int y) { int temp, i = 0; while (true) { temp = 1 << i; if (temp&x > temp&y) {

USACO Section 2.2 Party Lamps

/* ID: lucien23 PROG: lamps LANG: C++ */ /* * 此题的技巧之处就是需要注意到任何button只要按下2的倍数次就相当于没有按 * 所以其实只需要考虑4个按钮,每个按钮是否被有效按下过一次就好 * 直接使用枚举法,一共只有2^4=16种情况 * 对于每种情况需要知道被按下的有效次数(也就是被按下过的按钮数),必须满足 * (C-有效次数)%2=0才行,这样其他次数才能视为无效 * 然后验证各种情况是否符合要求,将符合要求的情况按序输出即可 */ #inc

USACO Section 2.2 Runaround Numbers

/* ID: lucien23 PROG: runround LANG: C++ */ #include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { ifstream infile("runround.in"); ofstream outfile("runround.out"); if(!infile || !

USACO Section 2.2 Preface Numbering

/* ID: lucien23 PROG: preface LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; int main() { ifstream infile("preface.in"); ofstream outfile("preface.out")

USACO Ordered Fractions

题目大意:求一个n的farey序列 思路:懒得麻烦的推导和公式了,暴力压倒一切 /*{ ID:a4298442 PROB:frac1 LANG:C++ } */ #include<cstdio> #include<iostream> #include<algorithm> #include<fstream> #define maxn 160*160+10 using namespace std; ifstream fin("frac1.in&quo