Count on Canton

来源:互联网 发布:西安星河网络小贷 编辑:程序博客网 时间:2024/06/06 00:29
Count on Canton
Time Limit: 1000MS
Memory Limit: 30000KTotal Submissions: 9142
Accepted: 5357

Description

One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below. 
1/1 1/2 1/3 1/4 1/5 ...2/1 2/2 2/3 2/43/1 3/2 3/34/1 4/25/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.

Input

The input list contains a single number per line and will be terminated by endof-file.

Output

You are to write a program that will read a list of numbers in the range from 1 to 10^7 and will print for each number the corresponding term in Cantor's enumeration as given below.

Sample Input

3147

Sample Output

TERM 3 IS 2/1TERM 14 IS 2/4TERM 7 IS 1/4

Source

Southeastern Europe 2004

<strong><span style="color:#009900;"><span style="font-size:14px;">#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>using namespace std;int a[1000000];int main(){    int i,n,m,k,x,y;    a[1]=1;    for(i=2;i<=1000000;i++)    {        a[i]=a[i-1]+i;//将每一斜层的和求出    }    while(scanf("%d",&n)!=EOF)    {        for(i=1;i<=n;i++)        {            if(n>=a[i]&&n<=a[i+1])//判断属于哪一层            {                m=n-a[i];                k=i;                break;            }        }        x=k+2-m;y=m;        if(k%2==0) printf("TERM %d IS %d/%d \n",n,x,y);        else printf("TERM %d IS %d/%d \n",n,y,x);    }</span>}</span></strong>


0 0
原创粉丝点击