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 ;
}

最新文章

  1. JAVA NIO Scatter/Gather(矢量IO)
  2. eclipse中安装Open Explorer
  3. 针对安卓java入门:数据类型
  4. 在sql-server上建立mysql链接库
  5. mac eclipse 安装maven,svn插件
  6. hg211g破解获取管理员密码,可以连接路由器。默认光猫来拨号。
  7. Hacking Secret Ciphers with Python翻译序言
  8. 预处理命令#define #undef #if #endif 的基本用法
  9. MCS-51特殊功能寄存器(SPR)的C51定义
  10. PHP商城购物车类
  11. War文件部署(转)
  12. angular drag and drop (ngDraggable) 笔记
  13. MSSQL的简单盲注
  14. Control-Tree
  15. php毫秒时间戳
  16. LintCode 387: Smallest Difference
  17. http://www.cnblogs.com/snake-hand/p/3206655.html
  18. 记录:C#监视某个文件的打开记录
  19. 加油吧 骚年QAQ
  20. 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)

热门文章

  1. Java 系列之spring学习--注解(三)
  2. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)
  3. Redhat/CentOS xfs文件系统及磁盘挂载
  4. VS2012 编译 boost1.53/ boost1.49
  5. C#获取硬盘序列号
  6. 【WebApp】IOS兼容问题
  7. Windows自调试Redis
  8. css3实现滚动手表
  9. javascript的带操作符的赋值运算
  10. python 获取excel数据 自动登陆