数据结构实验之链表八:Farey序列
来源:互联网 发布:西瓜影音官网mac版 编辑:程序博客网 时间:2024/05/17 04:07
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
Output
Example Input
6
Example Output
0/1 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/44/5 5/6 1/1
#include <iostream>#include<bits/stdc++.h>using namespace std;struct node{ int data1; int data2; node *next;};node *head,*tail,*p,*q;void chushihua(node *head){ p=new node; p->next=NULL; p->data1=0; p->data2=1; tail->next=p; tail=p; p=new node; p->next=NULL; p->data1=1; p->data2=1; tail->next=p; tail=p;}node * insert(node *head,int n){ p=head->next; tail=p->next; while(p) { if(tail==NULL) break; if((p->data2+tail->data2)<=n) { q=new node; q->data1=p->data1+tail->data1; q->data2=p->data2+tail->data2; q->next=p->next; p->next=q; tail=q; } else { p=p->next; tail=tail->next; } } return head;}void print(node *head){ int k=1; p=head->next; int count=0; while(p) { if(k) k=0; else cout<<"\t"; count++; // printf("%d/%d",p->data1,p->data2); cout<<p->data1<<"/"<<p->data2; p=p->next; if(count==10) { count=0; cout<<endl; k=1; } } if(count!=10) cout<<endl;}int main(){ int n; while(cin>>n) { head=new node; head->next=NULL; tail=head; chushihua(head); if(n==1) print(head); else { insert(head,n); print(head); } } return 0;}
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列 C++
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- [CSS]The space in the html.
- UVALive - 7045 Last Defence (更相减损术)
- python菜鸟学习之路 - 第一个游戏001
- 设计模式-依赖倒置原则
- 机械制造行业管理特点
- 数据结构实验之链表八:Farey序列
- rabbitmq消息转发模式1
- 读书笔记之《重构》第三章—代码的坏味道
- 随意大小的菱形打印c程序(Windows可运行),水平太差,无线for循环
- Java随机生成验证码,并且转化成图片
- 【深度学习】常见优化算法
- View绘制流程与源码分析
- sql子查询
- linux shell if 参数