您的位置:首页 > 其它

Count on Canton

2014-07-29 14:18 357 查看
A - Count on CantonTime Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uSubmit StatusDescriptionOne 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/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.InputThe input list contains a single number per line and will be terminated by endof-file.OutputYou 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
3
14
7
Sample Output
TERM 3 IS 2/1
TERM 14 IS 2/4
TERM 7 IS 1/4
这个题需要根据题目提供的前五个数据,找出规律,从1/1开始,到1/2,在左斜着到2/1,是一个S型,斜着的每一个分子分母的和是个定值,且递增,判断时需要判断奇偶,来确定分子分母的变化
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;int main(){    int n,m,i,k;    while(~scanf("%d",&n))    {        m=n;        int t=1;        for(i=1;i<10000000;i++)        {            m=m-i;            if(m<=i+1)                {                    //printf("i=%d\n",i);                    t=i+1;                    break;                }        }        int x=1,y=1;        if(n==1)            printf("TERM %d IS 1/1\n",n);        else        {        if(t%2==0)        {            x=t;            y=1;            for(i=1;i<=t;i++)            {                if(i==m)                    printf("TERM %d IS %d/%d\n",n,y,x);               x--;               y++;            }        }        else        {            x=1;            y=t;            for(i=1;i<=t;i++)            {                if(i==m)                    printf("TERM %d IS %d/%d\n",n,y,x);                  x++;                  y--;            }        }     //   printf("%d  m=%d\n",t,m);    }    }    return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: