[ACM]Q10408: Farey sequences
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q10408
{
class Program
{
static private int _n, _q; static private bool ntof, qtof; static private string[] sarr; static private double[] darr;
static private string[] sarr2; static private double[] darr2;
static void Main(string[] args)
{
for (; ; )
{
labeln: Console.Write("N>>>");
ntof = int.TryParse(Console.ReadLine(), out _n);
if (!ntof) goto labeln;
sarr = new string[(int)(_n * _n * 0.4)]; darr = new double[(int)(_n * _n * 0.4)];
int count = 1; darr[0] = 1; sarr[0] = "1/1";
for (int i = 1; i <= _n; i++)
for (int j = 1; j <= i; j++)
if (GCD(i, j)) { darr[count] = (double)j / (double)i; sarr[count] = j + "/" + i; count++; }
int amount = 0; foreach (double obj in darr) amount += obj == 0 ? 0 : 1;
sarr2 = new string[amount]; darr2 = new double[amount];
amount = 0;
for (int i = 0; i < sarr.Length; i++)
{
if (darr[i] != 0)
{
darr2[amount] = darr[i]; sarr2[amount] = sarr[i]; amount++;
}
}
Array.Sort(darr2, sarr2);
labelq: Console.Write("Question>>>");
qtof = int.TryParse(Console.ReadLine(), out _q);
if (!qtof) goto labelq;
Console.WriteLine("Ans={0}", sarr2[_q - 1]);
}
}
static public bool GCD(int x, int y)
{
while (true)
{
if (x > y) x = x % y;
else if (x < y) y = y % x;
else { return false; }
if (x == 1 || y == 1) { return true; }
else if (x == 0 || y == 0) { return false; }
}
}
}
}