P1014 Cantor表

xiaoxiao2021-02-27  219

题目描述

现代数学的著名证明之一是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,…

输入输出格式

输入格式: 整数N(1≤N≤10000000)

输出格式: 表中的第N项

输入输出样例

输入样例#1: 7 输出样例#1: 1/4

1.找规律 可以发现,第I个斜行的排列与其元素数字有关系 但奇数行和偶数行规律不一样,需分类讨论。 具体看代码 2.写成分数是要把原数颠倒 比如第7个 4/1 写成1/4

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdio> using namespace std; inline int read(){ char c; int res,flag = 0; while((c = getchar())>'9'||c<'0') if(c=='-')flag = 1; res = c - '0'; while((c = getchar())>='0'&&c<='9') res =(res<<3)+(res<<1) + c - '0'; return flag?-res:res; } int main(){ int n; n = read(); if(n==1){ cout<<"1/1"; return 0; } int x = 0; int t = 1; while(x < n){ x += t; t++; } t--; int m = abs(x - n); if(t%2==0){ cout<<t-m<<"/"<<m+1; }else{ cout<<m+1<<"/"<<t-m; } }
转载请注明原文地址: https://www.6miu.com/read-10748.html

最新回复(0)