您的位置:首页 > 理论基础 > 数据结构算法

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

2016-10-08 22:13 435 查看
数据结构实验之链表八:Farey序列

#include<bits/stdc++.h>

using namespace std;

struct node
{
int a,b;
node *next;
};

void creat(node *head,int n)
{
node *t,*p,*q;
t=head->next;
while (t->next!=NULL)
{
p=t->next;
if (t->b+p->b<=n)
{
q=new node;
q->a=p->a+t->a;
q->b=p->b+t->b;
t->next=q;
q->next=p;
}
t=t->next;
}
}

void show(node *head)
{
node *t;
t=head->next;
int k=0;
while (t!=NULL)
{
k++;
if (k%10==0)
printf("%d/%d\n",t->a,t->b);
else
printf("%d/%d\t",t->a,t->b);
t=t->next;
}
}

int main()
{
int n;
scanf("%d",&n);
node *head,*p,*q;
head=new node;
head->next=NULL;
p=new node;
q=new node;
p->a=0;
p->b=1;
q->a=1;
q->b=1;
head->next=p;
p->next=q;
q->next=NULL;
for (int i=2;i<=n;i++)
{
creat(head,i);
}
show(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: