Ordered Fractions

  Source : Unknown
  Time limit : 3 sec   Memory limit : 32 M

Submitted : 1510, Accepted : 496

Consider the set of all reduced fractions between 0 and 1 inclusivewith 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.

INPUT FORMAT

Several lines with a single integer N in each line.Process to the end of file.

SAMPLE INPUT

5
5

OUTPUT FORMAT

One fraction per line, sorted in order of magnitude.print a blank line after each testcase.

SAMPLE OUTPUT

0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1 0/1
1/5
1/4
1/3
2/5
1/2
3/5
2/3
3/4
4/5
1/1
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int a,b;
double s;
}num[100010];
int cmp(node s1,node s2)
{
return s1.s<s2.s;
}
int gcd(int a,int b)
{ return b==0?a:gcd(b,a%b);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
if(gcd(i,j)==1)
{
num[cnt].a=j;
num[cnt].b=i;
num[cnt].s=j*1.0/i;
cnt++;
}
} }
sort(num,num+cnt,cmp);
for(int i=0;i<cnt;i++)
printf("%d/%d\n",num[i].a,num[i].b);
printf("\n");
}
return 0;
}

HIToj--1076--Ordered Fractions(水题)的更多相关文章

  1. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  2. 洛谷——P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...

  3. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  5. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  6. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  7. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  8. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  9. 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题

    B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大 ...

随机推荐

  1. [SHOI2012]魔法树

    题目:洛谷P3833. 题目大意:给你一棵树,有两种操作:1.给两个点和它们之间的最短路上的所有点加上一个值:2.询问以某个点为根的子树的子树和.你需要实现这个功能. 解题思路:如果只有最后才询问的话 ...

  2. BZOJ 1085 / LOJ 2151 [SCOI2005]骑士精神 (剪枝/A*迭代搜索)

    题目大意:略 直接爆搜会T,我们优化一下,统计出当前棋盘和目标棋盘不同的位置的数量k,那么当前棋盘变成目标棋盘最少的移动次数是k-1 每次选择一个最大深度ma,那么如果当前走了dep步,显然必须保证d ...

  3. 收集整理的openstack java封装 api的第三方实现的选择

    Apache jclouds 地址:http://jclouds.apache.org/guides/openstack/ 一个开源库,java实现,支持cloudstack,openstack以及各 ...

  4. java里的一些特别值得注意的地方

    return 语句的作用:1.返回值 2.结束某个方法的执行. 局部变量必需要初始化,全局变量系统会默认初始值: 整型数赋默认值为0. 浮点数赋默认值为0.0,boolean赋默认值为false. c ...

  5. [React] Use React Fragments to make your DOM tree cleaner

    In this lesson, we will look at Fragments and how they are useful in achieving a cleaner DOM structu ...

  6. [TypeScript] Asynchronous Iteration using for-await-of

    The for-await-of syntax is similar to the for-of iteration. The key difference is that it automatica ...

  7. js php 数组比較

    php 与 javascript 数组除了定义以及 操作上有非常大的差别,还有非常多其他的差别.如今我们就来讨论讨论.    1.大家都知道php比較两个数组是否全相等(值,索引)相等 $a=arra ...

  8. LightOJ - 1132 Summing up Powers 矩阵高速幂

    题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...

  9. zookeeper应用场景练习(分布式锁)

    在寻常的高并发的程序中.为了保证数据的一致性.因此都会用到锁.来对当前的线程进行锁定.在单机操作中.非常好做到,比方能够採用Synchronized.Lock或者其它的读写多来锁定当前的线程.可是在分 ...

  10. HDU 1542 Atlantis(矩形面积并)

    HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...