数据结构实验之链表八:Farey序列
来源:互联网 发布:java word svg 编辑:程序博客网 时间:2024/06/05 20:34
数据结构实验之链表八:Farey序列
Time Limit: 10MS Memory Limit: 600KB
SubmitStatistic
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
Hint
Author
思路:设置好初值就可以了,按照题目意思打
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef int status;
typedef int element;
struct mode
{
element data1,data2;
struct mode *next;
};
void show(struct mode *head)
{
int c=0;
struct mode *p;
p=head->next;
while(p!=NULL)
{
c++;
if(c%10==0)
printf("%d/%d\n",p->data1,p->data2);
else
printf("%d/%d\t",p->data1,p->data2);
p=p->next;
}
}
int main()
{
struct mode *q,*r,*p,*head;
int n;
cin>>n;
head=(struct mode *)malloc(sizeof(struct mode));
p=(struct mode *)malloc(sizeof(struct mode));
head->next=p;
p->data1=0;
p->data2=1;
q=(struct mode *)malloc(sizeof(struct mode));
q->data1=1;
q->data2=1;
p->next=q;
q->next=NULL;
for(int i=2;i<=n;i++)
{
p=head->next;
while(p->next!=NULL)
{
q=p->next;
if(p->data2+q->data2<=i)
{
r=(struct mode *)malloc(sizeof(struct mode));
r->data1=p->data1+q->data1;
r->data2=p->data2+q->data2;
r->next=q;
p->next=r;
}
else
p=p->next;
}
}
show(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef int status;
typedef int element;
struct mode
{
element data1,data2;
struct mode *next;
};
void show(struct mode *head)
{
int c=0;
struct mode *p;
p=head->next;
while(p!=NULL)
{
c++;
if(c%10==0)
printf("%d/%d\n",p->data1,p->data2);
else
printf("%d/%d\t",p->data1,p->data2);
p=p->next;
}
}
int main()
{
struct mode *q,*r,*p,*head;
int n;
cin>>n;
head=(struct mode *)malloc(sizeof(struct mode));
p=(struct mode *)malloc(sizeof(struct mode));
head->next=p;
p->data1=0;
p->data2=1;
q=(struct mode *)malloc(sizeof(struct mode));
q->data1=1;
q->data2=1;
p->next=q;
q->next=NULL;
for(int i=2;i<=n;i++)
{
p=head->next;
while(p->next!=NULL)
{
q=p->next;
if(p->data2+q->data2<=i)
{
r=(struct mode *)malloc(sizeof(struct mode));
r->data1=p->data1+q->data1;
r->data2=p->data2+q->data2;
r->next=q;
p->next=r;
}
else
p=p->next;
}
}
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序列
- 读《一只 iPhone 的全球之旅》
- 时刻提醒自己,你为什么在这,你放弃了什么
- 安装mysql 5.7.19 winx64解压缩版
- CodeForces-171D Broken checker
- 模拟字符设备驱动的实现
- 数据结构实验之链表八:Farey序列
- Java中Split函数的用法技巧
- 【BigHereo 21】---T5-《信息系统开发与管理》---系统分析
- Eclipse各版本分析比较
- Linux下编译Android ffmpeg库(mediacodec 硬件解码)
- LeetCode71 Simplify Path
- 稳定匹配算法python实现
- 运行 Unix网络编程 中的第一个例子 unp学习 实例的运行
- HDU4472 Count (递推)