数据结构实验之链表八:Farey序列

Time Limit: 10 ms Memory Limit: 600 KiB

Submit Statistic Discuss

Problem Description

Farey序列是一个这样的序列:其第一级序列定义为(0/1,1/1),这一序列扩展到第二级形成序列(0/1,1/2,1/1),扩展到第三极形成序列(0/1,1/3,1/2,2/3,1/1),扩展到第四级则形成序列(0/1,1/4,1/3,1/2,2/3,3/4,1/1)。以后在每一级n,如果上一级的任何两个相邻分数a/c与b/d满足(c+d)<=n,就将一个新的分数(a+b)/(c+d)插入在两个分数之间。对于给定的n值,依次输出其第n级序列所包含的每一个分数。

Input

输入一个整数n(0<n<=100)

Output

依次输出第n级序列所包含的每一个分数,每行输出10个分数,同一行的两个相邻分数间隔一个制表符的距离。

Sample Input

6

Sample Output

0/1   1/6   1/5   1/4   1/3   2/5   1/2   3/5   2/3   3/4
4/5 5/6 1/1
#include <stdio.h>
#include <stdlib.h> struct node
{
int mu, zi;
struct node *next;
}; struct node *Init( )
{
struct node *head = ( struct node * )malloc(sizeof( struct node ));
struct node *p = (struct node *)malloc(sizeof(struct node ));
struct node *q = (struct node *)malloc(sizeof(struct node ));
head->next = p;
p->zi = 0;
p->mu = 1; p->next = q;
q->next = NULL;
q->zi = 1;
q->mu = 1;
return head; }; struct node *Create( int n )
{
int i;
struct node *head = Init( );
struct node *p, *q, *r; for( i=2; i<=n; i++ )
{
p = head->next;
q = p->next;
while( q )
{
if(p->mu + q->mu <= i)
{
r = (struct node *)malloc(sizeof( struct node ));
r->mu = p->mu + q->mu;
r->zi = p->zi + q->zi;
p->next = r;
r->next = q;
}
p = q;
q = q->next;
} }
return head;
}; void Output( struct node *head )
{
int cnt = 1;
struct node *p = head->next;
while( p->next )
{
printf("%d/%d", p->zi, p->mu );
if( ! ( cnt % 10 ) ) printf("\n");
else printf("\t");
cnt++;
p = p->next;
} printf("%d/%d\n", p->zi, p->mu );
} int main ( )
{
int n;
scanf("%d", &n);
struct node *head;
head = Create( n );
Output( head );
}

SDUT OJ 数据结构实验之链表八:Farey序列的更多相关文章

  1. SDUT OJ 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  2. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  3. SDUT OJ 数据结构实验之排序八:快速排序

    数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...

  4. SDUT OJ 数据结构实验之链表九:双向链表

    数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  5. SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除

    数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  6. SDUT OJ 数据结构实验之链表六:有序链表的建立

    数据结构实验之链表六:有序链表的建立 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

  7. SDUT OJ 数据结构实验之链表五:单链表的拆分

    数据结构实验之链表五:单链表的拆分 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. SDUT OJ 数据结构实验之链表四:有序链表的归并

    数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

  9. SDUT OJ 数据结构实验之链表三:链表的逆置

    数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

随机推荐

  1. struts2中s:iterator的使用(2个list嵌套循环)

    <s:iterator value="packagePlateTbls" id="plateTbls"> <tr> <td cla ...

  2. UML类图介绍及简单用法

    原文链接 一.类的属性的表示方式 在UML类图中,类使用包含类名.属性(field) 和方法(method) 且带有分割线的矩形来表示,比如下图表示一个Employee类,它包含name,age和em ...

  3. Spring总结二:IOC(控制反转)xml方式

    1,简介: IoC :Inverse of control 控制反转 ,思想就是在项目中引入一个工厂容器,对项目中接口依赖对象的创建,实现项目中对于依赖对象解耦合. 将程序中对象的创建权以及对象的整个 ...

  4. 思考ASP.NET网站静态化的利与弊

    最近在思考网站要不要进行静态化的问题,在网上收集和整理了有关静态化利与弊的资料,于是写下此博文分享到网络上.由于本人是一名asp.net开发人员,所以本文的观点可能无法涉及到全部方面,但是比较注重于使 ...

  5. javascript-文档结构遍历

    1.document.all document.all[0] //文档中第一个元素 document.all["navbar"] //id或name为"navbar&qu ...

  6. 使用Dom4j操作XML数据

    --------------siwuxie095                             dom4j 是一个非常优秀的 Java XML 的 API, 用来读写 XML 文件 和操作 ...

  7. jmap, jhat命令

    jmap命令有下面几种常用的用法 jmap [pid] jmap -histo:live [pid] >a.log jmap -dump:live,format=b,file=xxx.xxx [ ...

  8. SpringBoot17 FastJson配置、Druid配置

    1 FastJson配置 1.1 FastJson基础知识 点击前往 1.2 SpringBoot整合FastJson 点击前往 1.2.1 导入FastJson依赖 <!--fastjson- ...

  9. 136. Single Number唯一的一个只出现了一次的数字

    [抄题]: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  10. jsp Ajax请求(返回xml数据类型)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...