题目描述
现代数学的著名证明之一是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
每一层排列如下
第1层1/1
第2层1/2 2/1
第3层3/1 2/2 1/3
第4层1/4 2/3 3/2 4/1
第5层5/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;
}
}