toj 2798 Farey Sequence
2009-05-15 17:15
218 查看
Farey Sequence
Time Limit: 3.0 Seconds Memory Limit: 65536K Multiple test files
The Farey Sequence Fn for any integer n with n ≥ 2 is the set of irreducible rational numbers a/b with 0 < a < b ≤ n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
Now, your task is to print Farey Sequence given the value of n.
Source: The 5th UESTC Programming Contest
Problem ID in problemset: 2798
Submit Back Runs Statistics Clarifications
#include <iostream>
#include <queue>
using namespace std;
int t,n;
typedef struct node
{
short a,b;
float c;
node(){}
node(short aa,short bb,float cc)
{
a=aa;
b=bb;
c=cc;
}
friend bool operator <(node x,node y)
{
return x.c>y.c;
}
}Point;
priority_queue<Point>Q;
Point p;
int gcd (short a , short b)
{
if (b == 0)
return a;
return gcd (b , a % b);
}
int main()
{
short i,j;
scanf("%d",&t);
// int num = 0;
while(t--)
{
scanf("%d",&n);
printf("1/%d",n);
while(!Q.empty())
Q.pop();
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
{
if(i==1&&j==n)
continue;
if(gcd(i,j)==1)
{
Q.push(node(i,j,i*1.0/j));
// num ++;
}
}
while(!Q.empty())
{
p=Q.top();
Q.pop();
printf(",%d/%d",p.a,p.b);
}
//cout<<num<<endl;
printf("\n");
}
return 0;
}
Time Limit: 3.0 Seconds Memory Limit: 65536K Multiple test files
The Farey Sequence Fn for any integer n with n ≥ 2 is the set of irreducible rational numbers a/b with 0 < a < b ≤ n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
Now, your task is to print Farey Sequence given the value of n.
Input
There are several test cases. The first line is an integer giving the number of cases. Each test case has only one line, which contains a positive integer n.Output
For each test case, you should output one line, which contains the corresponding Farey Sequence. Adjacent terms are separated by a single ',' and there can't be any white spaces in your output. See Sample Output for more clarifications on the output format.Constraints
2 ≤ n ≤ 3000Sample Input
4 2 3 4 5
Sample Output
1/2 1/3,1/2,2/3 1/4,1/3,1/2,2/3,3/4 1/5,1/4,1/3,2/5,1/2,3/5,2/3,3/4,4/5
Note
Do Not use cout to produce the output for this problem, since it is inefficient.Source: The 5th UESTC Programming Contest
Problem ID in problemset: 2798
Submit Back Runs Statistics Clarifications
#include <iostream>
#include <queue>
using namespace std;
int t,n;
typedef struct node
{
short a,b;
float c;
node(){}
node(short aa,short bb,float cc)
{
a=aa;
b=bb;
c=cc;
}
friend bool operator <(node x,node y)
{
return x.c>y.c;
}
}Point;
priority_queue<Point>Q;
Point p;
int gcd (short a , short b)
{
if (b == 0)
return a;
return gcd (b , a % b);
}
int main()
{
short i,j;
scanf("%d",&t);
// int num = 0;
while(t--)
{
scanf("%d",&n);
printf("1/%d",n);
while(!Q.empty())
Q.pop();
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
{
if(i==1&&j==n)
continue;
if(gcd(i,j)==1)
{
Q.push(node(i,j,i*1.0/j));
// num ++;
}
}
while(!Q.empty())
{
p=Q.top();
Q.pop();
printf(",%d/%d",p.a,p.b);
}
//cout<<num<<endl;
printf("\n");
}
return 0;
}
相关文章推荐
- TOJ_1001解题报告
- TOJ 2199 A+B Problem的无奈 2006-07-21
- toj 2815 Searching Problem
- toj 1709 Incomplete chess boards
- toj 2975 Encription
- TOJ 1132 Knight Moves BFS求最短路径
- toj 3474 解题报告
- [TOJ]1151. Task Sequences [zoj]3332.Strange Country II --竞赛图的哈密顿路
- TOJ 2241. Ancestor 【判断一棵树上两个节点的关系】
- TOJ 3622. Perfect Matching
- TOJ 1022
- TOJ 1398 POJ 2362 Square DFS
- TOJ 3853 判断点是否在凸包内
- TOJ 1036
- Stock Exchange POJ 3903/ TOJ 3120
- TOJ 10008 a^b mod c
- TOJ 2380 POJ 2536 Gopher II /二分图
- TOJ 1225 数据结构练习题——Huffman Coding
- TOJ 3667 HDU 2830 Matrix Swapping II
- TOJ 1837 HDU 1385 ZOJ 1456 Minimum Transport Cost / 最短路径