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

来源:互联网 发布:网络上赌博输了十几万 编辑:程序博客网 时间:2024/04/29 12:30

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

Time Limit: 10MS Memory Limit: 600KB
Submit Statistic

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个分数,同一行的两个相邻分数间隔一个制表符的距离。

Example Input

6

Example Output

0/1   1/6   1/5   1/4   1/3   2/5   1/2   3/5   2/3   3/44/5   5/6   1/1


#include <stdio.h>#include <stdlib.h>typedef int elemtype;typedef int status;typedef struct node{    elemtype data1, data2;    node *next;} node, *linklist;linklist create(linklist head, int n){    linklist tail1, tail2;    head = (node *)malloc(sizeof(node));    head->next = NULL;    tail1 = (node *)malloc(sizeof(node));    tail2 = (node *)malloc(sizeof(node));    tail1->data1 = 0;    tail1->data2 = 1;    tail2->data1 = 1;    tail2->data2 = 1;    head->next = tail1;    tail1->next = tail2;    tail2->next = NULL;    linklist p, q, s;    for(int i=2; i<=n; i++)    {        p = head->next;        while(p->next!=NULL)        {            q = p->next;            if(p->data2+q->data2<=i)            {                s = (node *)malloc(sizeof(node));                s->data1 = p->data1 + q->data1;                s->data2 = p->data2 + q->data2;                p->next = s;                s->next = q;            }            p = p->next;        }    }    return head;}void display(linklist head){    linklist p;    p = head->next;    int count = 0;    while(p)    {        count++;        if(count%10==0)            printf("%d/%d\n", p->data1, p->data2);        else            printf("%d/%d\t", p->data1, p->data2);        p = p->next;    }}int main(){    int n;    scanf("%d", &n);    linklist head;    head = create(head, n);    display(head);    return 0;}


0 0