Code Vs-problem-1083 Cantor表

来源:互联网 发布:golang kafka 编辑:程序博客网 时间:2024/05/29 13:29

题目描述 Description

现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的: 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 … … 我们以Z字形给上表的每一项编号。第一项是1/1,然后是1/2,2/1,3/1,2/2,…

输入描Input Description

整数N(1≤N≤10000000)

输出描述Output Description

表中的第N项

样例输入Sample Input

7

样例输出Sample Output

1/4

思路

简单的数学问题,我将其理解为金字塔,从上而下第x层有x个空间,蛇形迂回向下。

#include<iostream>using namespace std;int main(){    long n,x,left;    cin>>n;    for(x=1;;x++)       if((x*(x-1)/2+1<=n)&&(x*(x+1)/2>=n)) break;      if(x*(x+1)/2>n)       left=(n-x*(x-1)/2)%x-1;    else                                                left=x-1;    if(x%2==0)       cout<<1+left<<"/"<<x-left;    else       cout<<x-left<<"/"<<1+left;    return 0;}



0 0
原创粉丝点击