洛谷P1014 Cantor表

题目描述
现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的:

1/11/1 , 1/21/2 , 1/31/3 , 1/41/4, 1/51/5, …

2/12/1, 2/22/2 , 2/32/3, 2/42/4, …

3/13/1 , 3/23/2, 3/33/3, …

4/14/1, 4/24/2, …

5/15/1, …

… 我们以ZZ字形给上表的每一项编号。第一项是1/11/1,然后是1/21/2,2/12/1,3/13/1,2/22/2,…

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

输出格式
表中的第NN项

输入输出样例
输入 #1复制
7
输出 #1复制
1/4
在这里插入图片描述
每一层排列如下

11/121/2 2/133/1 2/2 1/341/4 2/3 3/2 4/155/1 4/2 3/3 2/4 1/5

所以,输入n后先判断是第几层的第几个。
如果是奇数层,就从i/1开始,分子递减分母递增。偶数层则相反

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
using namespace std;
#define dd double
#define ll long long
const ll MAXN = 1e3 + 5;
const ll INF = 1e18;
const ll shit = 1e9 + 7;
#define f(i, x, y) for(ll i = x; i < y; i++)
dd PI = acos(-1);
//ios::sync_with_stdio(false);
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	ll n; cin >> n;
	ll k = 1;
	while (n > k) {
		n -= k;
		k++;
	}
	//cout << k << endl;
	//cout << n << endl;
	if (k % 2 == 0) {
		cout << n << "/" << k - n + 1 << endl;
	}
	else {
		cout << k - n + 1 << "/" << n << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值