数据结构实验之链表八:Farey序列
来源:互联网 发布:医疗答题软件 编辑:程序博客网 时间:2024/04/29 08:39
数据结构实验之链表八: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/44/5 5/6 1/1
#include <bits/stdc++.h>using namespace std;struct node{ int a, b; node *next;};struct node* creat(int n){ node *head, *p, *q, *t; head = new node; head->next = NULL; p = new node; p->a = 0; p->b = 1; p->next = NULL; q = new node; q->a = 1; q->b = 1; q->next = NULL; head->next = p; p->next = q; for(int i = 1; i <= n; i++) { p = head->next; q = p->next; while(q) { if(p->b+q->b <= i) { t = new node; t->a = p->a+q->a; t->b = p->b+q->b; p->next = t; t->next = q; p = q; q = q->next; } else { p = p->next; q = q->next; } } } return head;}int main(){ int n; cin >> n; node *head, *p; head = creat(n); int cnt=0; for(p = head->next; p; p=p->next) { if(cnt < 9) { cout << p->a << "/" << p->b << "\t"; cnt++; } else if(cnt == 9) { cout << p->a << "/" << p->b << endl; cnt = 0; } } return 0;}
0 0
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列 C++
- 数据结构实验之链表八:Farey序列
- 数据结构实验之链表八:Farey序列
- leetcode小白解题记录——第七题
- 有关C++面向对象面试题
- 不用输入法打字
- [完]Spark安装学习实践
- LeetCode[299] Bulls and Cows
- 数据结构实验之链表八:Farey序列
- 函数参数执行顺序
- 单片机独立按键
- cocos2d-x(7)——字体标签CCLabel(转载)
- JAVA调用动态链接库(dll)
- 如何成为一名黑客(网络安全从业者)——网络攻击技术篇(1/8 扫描技术)
- Java--自我心得之StringBuffer类,File类,字节流与字符流
- POJ 3273
- service服务程序