Ordered Fractions(USACO 2.1.2)

篩選法求素數的思想

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#define EPS 1e-9
//#define LOCAL

using namespace std;

#ifdef LOCAL
ofstream fout ("out.txt");
ifstream fin ("in.txt");
#else
ofstream fout ("frac1.out");
ifstream fin ("frac1.in");
#endif

struct node
{
	int x;
	int y;
	double v;
};

int ok[161][161];

node n[30000];

bool cmp(const node &a, const node &b)
{
	return (a.v - b.v) < -EPS;
}

int main()
{
	int node_num = 0;
	int m;
	fin>>m;
	//cout<<"2\n";

	for(int i = 0; i <= m; i++)
		for(int j = max(i, 1); j <= m; j++)
		{
			if(!ok[i][j])
			{
				int k = 1;
				while(j*k <= m)
				{
					ok[i*k][j*k] = 1;
					k++;
				}

				n[node_num].x = i;
				n[node_num].y = j;
				n[node_num].v = (double)i/(j+0.0);
				node_num++;
			}
		}
    //cout<<"1\n";
	sort(n, n+node_num, cmp);
	for (int i = 0; i < node_num; ++i)
	{
		fout<<n[i].x<<'/'<<n[i].y<<endl;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章