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

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a1, a2;
    struct node *next;
};
int main()
{
    int n;
    scanf("%d", &n);
    struct node *h, *q, *p;
    h = (struct node *)malloc(sizeof(struct node));
    h -> next = NULL;
    q = h;
    p = (struct node *)malloc(sizeof(struct node ));
    p -> next = NULL;
    p -> a1 = 0; p -> a2 = 1;
    q -> next = p;
    q = p;
    p = (struct node *)malloc(sizeof(struct node ));
    p -> next = NULL;
    p -> a1 = 1; p -> a2 = 1;
    q -> next = p;
    q = p;
    if(n > 1)
    {
        for(int k = 2; k <= n; k++)
        {
            p = h -> next;
            while(p && p -> next)
            {
                int a = p -> a1, c = p -> a2, b = p -> next -> a1, d = p -> next -> a2;
                if(c + d <= n)
                {
                    int x = a + b, y = c + d;
                    q = (struct node *)malloc(sizeof(struct node ));
                    q -> a1 = x; q -> a2 = y;
                    q -> next = p -> next;
                    p -> next = q;
                    p = p -> next -> next;
                }
                else
                    p = p -> next;
            }

        }
    }
    int num = 1;
    p = h -> next;
    printf("%d/%d", p -> a1, p -> a2);
    p = p -> next;
    int flag = 0;
    while(p)
    {
        if(num % 10 == 0){flag = 1; printf("\n");}
        if(flag == 0)printf("\t%d/%d", p -> a1, p -> a2);
        else {flag = 0; printf("%d/%d", p -> a1, p -> a2);}
        num++;
        p = p -> next;
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值