SDUT OJ 数据结构实验之链表八:Farey序列
数据结构实验之链表八:Farey序列
Time Limit: 10 ms Memory Limit: 600 KiB
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 <stdio.h>
#include <stdlib.h>
struct node
{
int mu, zi;
struct node *next;
};
struct node *Init( )
{
struct node *head = ( struct node * )malloc(sizeof( struct node ));
struct node *p = (struct node *)malloc(sizeof(struct node ));
struct node *q = (struct node *)malloc(sizeof(struct node ));
head->next = p;
p->zi = 0;
p->mu = 1;
p->next = q;
q->next = NULL;
q->zi = 1;
q->mu = 1;
return head;
};
struct node *Create( int n )
{
int i;
struct node *head = Init( );
struct node *p, *q, *r;
for( i=2; i<=n; i++ )
{
p = head->next;
q = p->next;
while( q )
{
if(p->mu + q->mu <= i)
{
r = (struct node *)malloc(sizeof( struct node ));
r->mu = p->mu + q->mu;
r->zi = p->zi + q->zi;
p->next = r;
r->next = q;
}
p = q;
q = q->next;
}
}
return head;
};
void Output( struct node *head )
{
int cnt = 1;
struct node *p = head->next;
while( p->next )
{
printf("%d/%d", p->zi, p->mu );
if( ! ( cnt % 10 ) ) printf("\n");
else printf("\t");
cnt++;
p = p->next;
}
printf("%d/%d\n", p->zi, p->mu );
}
int main ( )
{
int n;
scanf("%d", &n);
struct node *head;
head = Create( n );
Output( head );
}
SDUT OJ 数据结构实验之链表八:Farey序列的更多相关文章
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT OJ 数据结构实验之排序八:快速排序
数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...
- SDUT OJ 数据结构实验之链表九:双向链表
数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT OJ 数据结构实验之链表六:有序链表的建立
数据结构实验之链表六:有序链表的建立 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT OJ 数据结构实验之链表五:单链表的拆分
数据结构实验之链表五:单链表的拆分 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之链表四:有序链表的归并
数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT OJ 数据结构实验之链表三:链表的逆置
数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
随机推荐
- Minimum Sum of Array(map)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- 解决mybatis中转义字符的问题
xml格式中不允许出现类似“>”这样的字符,有如下两种解决方法 方法一:使用转义字符 SELECT * FROM test WHERE 1 = 1 AND start_date <= CU ...
- SpringMVC总结四:拦截器简单介绍
首先要说一下HandlerExecutionChain: HandlerExecutionChain是一个执行链,当用户的请求到达DispatcherServlet的时候,DispatcherServ ...
- 刷题向》关于第一篇状压DP BZOJ1087 (EASY+)
这是本蒟蒻做的第一篇状压DP,有纪念意义. 这道题题目对状压DP十分友善,算是一道模板题. 分析题目,我们发现可以用0和1代表每一个格子的国王情况, 题目所说国王不能相邻放置,那么首先对于每一行是否合 ...
- [luogu3369]普通平衡树(替罪羊树模板)
解题关键:由于需要根据平衡进行重建,所以不能进行去重,否则无法保证平衡性. #include<cstdio> #include<cstring> #include<alg ...
- 【bzoj3942】[Usaco2015 Feb]Censoring
[题目大意] 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程. [样例输入] whatth ...
- laravel 验证机制validation
Laravel 中 validation 验证 返回中文提示 全局设置 自己建一个zn文件夹,然后把en的4个文件全复制过去,修改validation.php的代码为下面的内容,然后在app.php修 ...
- Java 基于web service 暴露接口 供外部调用
package cn.zr.out.outinterface; import java.text.SimpleDateFormat; import java.util.Date; import jav ...
- 面试题:彻底理解ThreadLocal 索引的利弊 背1
.索引利弊 --整理 1.索引的好处 a.提高数据检索的效率,降低检索过程中必须要读取得数据量,降低数据库IO成本. b.降低数据库的排序成本.因为索引就是对字段数据进行排序后存储的,如果待排序的 ...
- jquery dropdownlist.js
$.fn.extend({ SetDict: function (option) { var txtControl = $(this); if (!txtControl.hasClass(" ...