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

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

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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int a,b;
    struct node *next;
};
int main()
{
    int n;
    scanf("%d",&n);
    struct node *head,*p,*tail,*q;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    p=(struct node *)malloc(sizeof(struct node));
    p->a=0;
    p->b=1;
    p->next=NULL;
    tail->next=p;
    tail=p;
    p=(struct node *)malloc(sizeof(struct node));
    p->a=1;
    p->b=1;
    p->next=NULL;
    tail->next=p;
    tail=p;
    for(int i=2;i<=n;i++)
    {
        p=head->next;
        while(p&&p->next)  //插入链表
        {
            if(p->b+p->next->b<=i)
            {
                q=(struct node *)malloc(sizeof(struct node));  //不要忘记开辟新空间
                q->a=p->a+p->next->a;
                q->b=i;
                q->next=p->next;
                p->next=q;
            }
            else p=p->next;
        }
    }
    int t=0;
    p=head->next;
    while(p)
    {
        t++;
        if(t==10) {printf("%d/%d\n",p->a,p->b);t=0;}
        else printf("%d/%d\t",p->a,p->b); //"\t"制表符
        p=p->next;
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    int a,b;
    struct node *next;
};
int main()
{
        int n;
        scanf("%d",&n);
        struct node *head,*p,*tail,*t,*q;
        head=(struct node *)malloc(sizeof(struct node));
        head->next=NULL;
        tail=head;
        p=(struct node *)malloc(sizeof(struct node));
        p->a=0;
        p->b=1;
        p->next=NULL;
        tail->next=p;
        tail=p;
        p=(struct node *)malloc(sizeof(struct node));
        p->a=1;
        p->b=1;
        p->next=NULL;
        tail->next=p;
        tail=p;
        for(int i=2;i<=n;i++)
        {
            p=head->next;
            q=p->next;
            while(p&&q)
            {
                if(p->b+q->b<=n)
                {
                    t=(struct node *)malloc(sizeof(struct node));
                    t->a=p->a+q->a;
                    t->b=p->b+q->b;
                    t->next=p->next;
                    p->next=t;
                }
                p=q;q=q->next;
            }
        }
        int c=0;
        p=head->next;
        while(p)
        {
            c++;
            if(c==10) {printf("%d/%d\n",p->a,p->b);c=0;}
            else printf("%d/%d\t",p->a,p->b);
            p=p->next;
        }
        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值