1/1 1/2 1/3 1/4 1/5 ...
2/1 2/2 2/3 2/4
3/1 3/2 3/3
4/1 4/2
5/1
In the above diagram, the first term is 1/1, the second term is 1/2, the third term is 2/1, the fourth term is 3/1, the fifth term is 2/2, and so on.
也就是
按 1/1 1/2 2/1 3/1 2/2 1/3。。。排序。。。规律很明显。
分子都是 1 1 2 1 2 3 1 2 3 4 1.。。。
分母都是 1 2 1 3 2 1 4 3 2 1 5.。。。。
很明显了吧。不过呢 是s型排列。。。所以呢这么求出来之后需要看在斜行的奇偶性,如果在偶行,需要把分子分母颠倒下。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <limits.h>
using namespace std;
int compute(int x)
{
int big = (int)sqrt(2*x);
int i;
for(i=big; i>=1; i++)
if( i*(i+1)/2 >= x && i*(i-1)/2 <= x )
break;
int a = x - i*(i-1)/2;
if( i % 2 == 1 )
printf("%d/%d/n",i-a+1,a);
else
printf("%d/%d/n",a,i-a+1);
}
int main()
{
int n, ind = 1;
while( ~scanf("%d",&n) )
{
printf("TERM %d IS ",n);
compute(n);
}
return 0;
}