数据结构实验之链表八:Farey序列
来源:互联网 发布:新能源就业前景 知乎 编辑:程序博客网 时间:2024/06/06 03:11
数据结构实验之链表八: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
#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{ int a, b; struct node* next;};struct node * creat(struct node * head, int n){ if(n!=1)//插入链表 { struct node * p , *q; p = head->next; q = p->next; while(q) { if(p->b+q->b<=n) { struct node * w; w = (struct node *)malloc(sizeof(struct node)); w->a = p->a+q->a; w->b = p->b+q->b; p->next = w; w->next = q; p = q; if(q->next) q = q->next; else q = NULL; } else { p = p->next; if(!q->next) q = NULL; else q = q->next; } } } else//开始的时候先建立链表0/1 1/1 { struct node *p, *q; p = (struct node *)malloc(sizeof(struct node)); q = (struct node *)malloc(sizeof(struct node)); q->a = 1; q->b = 1; p->a = 0; p->b = 1; q->next = NULL; head->next = p; p->next = q; } return head;};void show(struct node * head){ struct node * p = head->next; while(p) { for(int i=0;i<9;i++) { if(p->next) { printf("%d/%d\t", p->a, p->b);//注意制表符 p = p->next; } else if(!p->next) { printf("%d/%d\t", p->a, p->b); p = NULL; break; } } if(p==NULL) break; printf("%d/%d\n", p->a, p->b); if(p->next) p = p->next; else p = NULL; }}int main(){ int n; scanf("%d", &n); struct node * head; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; for(int i=1;i<=n;i++) { head = creat(head, i); } show(head); return 0;}
阅读全文
0 0
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列 C++
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- bzoj 2565 最长双回文串 马拉车
- 日志同步与时间同步
- SpringMVC4.3+ hibernate5 整合EhCache 二级缓存 给予java-config
- 发现一个很好的博客,连接是博客中一篇写三种梯度下降发的文章,收藏
- HSV与RGB
- 数据结构实验之链表八:Farey序列
- main :处理命令行选项
- c#使用WPD读取便携式设备信息一(枚举设备、连接设备及读取设备信息)
- sql server学习总结一
- hdu5976(前缀+逆元)
- zookeeper 安装的三种模式
- BZOJ 1003: [ZJOI2006]物流运输
- 暑期集训第一阶段总结(附带杂题总结+字符串总结)
- 将请求转换为json请求