zoukankan      html  css  js  c++  java
  • HIToj--1076--Ordered Fractions(水题)

    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
    ",num[i].a,num[i].b);
    		printf("
    ");
    	}
    	return 0;
    }



  • 相关阅读:
    mvn clean deploy
    数据库分库分表,读写分离
    耳鸣治疗法
    Navicat Preminum
    spring boot 获取bean
    java中集合Collection转list对象
    Java8新特性之Collectors
    spring 给一个类 生成test
    Spring注解标签详解@Autowired @Qualifier等 @Slf4j
    linux定时执行脚本
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273633.html
Copyright © 2011-2022 走看看