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

mac2022-06-30  20

数据结构实验之链表八: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/4 4/5 5/6 1/1

#include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef struct node { int a; int b; struct node *next; }node,*link; link creat(int n) { link p,q,tail,t,q1; q1=(link *)malloc(sizeof(node)); q1->a=1; q1->b=1; q1->next=NULL; p=(link *)malloc(sizeof(node)); p->a=0; p->b=1; p->next=q1; int i; for(i=2;i<=n;i++) { tail=p; while(tail->next) { q=tail->next; if(tail->b+q->b<=n) { t=(link )malloc(sizeof(node)); t->a=tail->a+q->a; t->b=tail->b+q->b; t->next=q; tail->next=t; } tail=tail->next; } } return p; } void put(link p) { int flag=1; while(p->next) { if(flag==0) { printf("%d",p->a); printf("/"); printf("%d",p->b); p=p->next; printf("\n"); flag++; } else { printf("%d",p->a); printf("/"); printf("%d",p->b); printf("\t"); p=p->next; flag++; } } if(flag==0) { printf("%d",p->a); printf("/"); printf("%d",p->b); p=p->next; printf("\n"); flag++; } else { printf("%d",p->a); printf("/"); printf("%d",p->b); printf("\n"); p=p->next; flag++; } } int main() { link p; int n; scanf("%d",&n); p=creat(n); put(p); return 0; }

转载于:https://www.cnblogs.com/CCCrunner/p/6444599.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)