数据结构实验之链表八: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. Java微信公众平台开发(十)--微信自定义菜单的创建实现

    转自:http://www.cuiyongzhi.com/post/48.html 自定义菜单这个功能在我们普通的编辑模式下是可以直接在后台编辑的,但是一旦我们进入开发模式之后我们的自定义菜单就需要自 ...

  2. c语言之字符串数组

    一.字符串与字符串数组 1.字符数组的定义 char array[100]; 2.字符数组初始化 char array[100] = {'a','b','c'};  //array[0] = 'a' ...

  3. 使对象可以像数组一样foreach循环,要求属性必须是私有的(写个类实现Iterator接口)

    <?php class Test implements Iterator { ,,,,); public function __construct() { } // 重置,将数组内部指针指向第一 ...

  4. IIS身份验证知识摘录

    IIS 身份验证 ASP.NET 身份验证分为两个步骤.首先,Internet 信息服务 (IIS) 对用户进行身份验证,并创建一个 Windows 令牌来表示该用户.IIS 通过查看 IIS 元数据 ...

  5. spark源码阅读之network(2)

    在上节的解读中发现spark的源码中大量使用netty的buffer部分的api,该节将看到netty核心的一些api,比如channel: 在Netty里,Channel是通讯的载体(网络套接字或组 ...

  6. HUSTSE2017级5班3组小组JIRA软件使用体验看法汇总--未订正版

    小组JIRA软件使用体验看法汇总 小黄 JIRA还是比较方便的,在项目组合管理阶段,可以让团队同时按时发布,也可以让计划变得更容易和快捷,可以跟踪团队的重要计划,清楚的了解项目的进度. 看了JIRA的 ...

  7. sql2008调试存储过程

    拿上篇存储过程为例: 在意个窗口里面写上exec Proc_MoveUpOrDown2 'id',3,1,'tableName,'orderid' 按F11,有个黄色的箭头会指向该行, 再按F11会跳 ...

  8. Python htmlTestRunner生成测试报告Demo

    #该代码段是ReadTxt_demo.py 的代码,用户读取txt 文件中的用户信息. #ReadTxt_demo.py def readTxt(filePath): fo = open(filePa ...

  9. POJ3020 Antenna Placement(二分图最小路径覆盖)

    The Global Aerial Research Centre has been allotted the task of building the fifth generation of mob ...

  10. MongoDB整理笔记の高级查询

    1.条件操作符 <, <=, >, >= 这个操作符就不用多解释了,最常用也是最简单的    db.collection.find({ "field" : ...