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 1 and 160 inclusive, prints the fractions in order of increasing magnitude.
PROGRAM NAME: frac1
INPUT FORMAT
One line with a single integer N.
SAMPLE INPUT (file frac1.in)
5
OUTPUT FORMAT
One fraction per line, sorted in order of magnitude.
SAMPLE OUTPUT (file frac1.out)
0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
题目大意:就是说给定一个N,输出值在0到1之间的,分母在1到N之间的所有值不重复的分数(可以约分的需要约分)。
思路:很简单,因为数据量小,所以就是枚举分子分母,然后不要不是最简分数的分数,再排序。
/*
ID:fffgrdc1
PROB:frac1
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int prime[],primecnt=;
bool bo[]={};
struct str
{
int x;int y;
double ans;
}e[];
bool kong(str aa,str bb)
{
return aa.ans<bb.ans;
}
bool check(int x,int y)
{
//int temp=sqrt(double (y));
for(int i=;i<=primecnt&&prime[i]<=y;i++)
{
if(!(y%prime[i]))
if(!(x%prime[i]))
return ;
}
return ;
}
int main()
{
freopen("frac1.in","r",stdin);
freopen("frac1.out","w",stdout);
int n;
scanf("%d",&n);
int anscnt=;
bo[]=bo[]=;
for(int i=;i<;i++)
{
if(!bo[i])
{
prime[++primecnt]=i;
for(int j=;j*i<;j++)
{
bo[i*j]=;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
if(check(j,i))
{
e[++anscnt].x=j;
e[anscnt].y=i;
e[anscnt].ans=(j*1.0)/i;
}
}
}
sort(e+,e++anscnt,kong);
printf("0/1\n");
for(int i=;i<=anscnt;i++)
{
printf("%d/%d\n",e[i].x,e[i].y);
}
printf("1/1\n");
return ;
}
check函数写的太烂了。。。WA了几发都是因为想优化它。本来是想到用GCD的,但是担心时间复杂度的问题,后来学长告诉我不用担心呀,而且甚至不用自己手写,algorithm里面有现成的。。。于是代码变成下面这样也A了,而且复杂度下降了。。。。惊了
/*
ID:fffgrdc1
PROB:frac1
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int prime[],primecnt=;
bool bo[]={};
struct str
{
int x;int y;
double ans;
}e[];
bool kong(str aa,str bb)
{
return aa.ans<bb.ans;
}
bool check(int x,int y)
{
return __gcd(x,y)==;
}
int main()
{
freopen("frac1.in","r",stdin);
freopen("frac1.out","w",stdout);
int n;
scanf("%d",&n);
int anscnt=;
bo[]=bo[]=;
for(int i=;i<;i++)
{
if(!bo[i])
{
prime[++primecnt]=i;
for(int j=;j*i<;j++)
{
bo[i*j]=;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
if(check(j,i))
{
e[++anscnt].x=j;
e[anscnt].y=i;
e[anscnt].ans=(j*1.0)/i;
}
}
}
sort(e+,e++anscnt,kong);
printf("0/1\n");
for(int i=;i<=anscnt;i++)
{
printf("%d/%d\n",e[i].x,e[i].y);
}
printf("1/1\n");
return ;
}
USACO 2.1 Ordered Fractions的更多相关文章
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 洛谷P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...
- 洛谷——P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- 洛谷 P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- 【USACO 2.1】Ordered Fractions
/* TASK: frac1 LANG: C++ URL: http://train.usaco.org/usacoprob2?S=frac1&a=dbgwn5v2WLr SOLVE: 直接枚 ...
- USACO Ordered Fractions
首先看一下题目 Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less t ...
- USACO Section 2.1 Ordered Fractions
/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...
- USACO Section 2.1 Ordered Fractions 解题报告
题目 题目描述 给定一个数N(1<=N<=160),需要产生所有的分数,这些分数的值必须要在0~1之间.而且每个分数的分母不能超过N.如下例所示: N = 5 产生所有的分数:0/1 1/ ...
- [Swust OJ 801]--Ordered Fractions
题目链接:http://acm.swust.edu.cn/problem/801/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
随机推荐
- C - Young Physicist
Problem description A guy named Vasya attends the final grade of a high school. One day Vasya decide ...
- Windows系统开发常用类-------------Environment类
Windows系统开发常用类-------------Environment类: SystemDirectory//显示系统目录 MachineName//计算机名称 ...
- equal height
https://css-tricks.com/the-perfect-fluid-width-layout/ http://nicolasgallagher.com/multiple-backgrou ...
- C# 4.0新加特性
协变和逆变 这个在C#2.0中就已经支持委托的协变和逆变了,C#4.0开始支持针对泛型接口的协变和逆变: IList<string> strings = new List<strin ...
- MD5加盐,实现一人一密
理论上md5是不可逆的,而且MD5本来也不是作加密使用,而是用来校验数据的完整性,只是因为其不可逆且稳定.快速的特点,被广泛用于对明文密码的加密. 至今仍然后很多开发人员相信MD5的保密性,也许因为他 ...
- 杭电1019 Least Common Multiple【求最小公倍数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...
- In-Out Parameters inout keyword
You write an in-out parameter by placing the inout keyword right before a parameter’s type. An in-ou ...
- Js判断一个字符串是否包含一个子串
Js中经常遇到判断一个字符串是否包含一个子串,java语言中有containes的方法,直接调用就可以了.除非引用第三方数据库,Js中没有contains方法. 为了实现更java语言中contain ...
- centos7常见的操作
centos7的网络IP地址配置文件在 /etc/sysconfig/network-scripts 文件夹下, 查看当前网卡名称 ip ad li ens33网卡对应的配置文件为ifcfg-ens ...
- vc++如何创建程序-函数的重载
重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载 函数重载分为两种情况: 1 .(1)void output(); (2)int output(); 2 .(1)void output ...