题目
原题地址
题目描述
现代数学的著名证明之一是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
题解
脑子锈住了,没找着规律,看题意都看了将近两个小时,写了个模拟过了,四种状态分开处理就好了。
代码
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int N = -1, left = 1, right = 1;
bool direction = true;
cin >> N;
for(int count = 1; count < N; count++){
if(left == 1 && direction == true){ //up and at edge
direction = !direction;
right += 1;
}
else if(right == 1 && direction == false){ //down and at edge
direction = !direction;
left += 1;
}
else if(direction){ //up
left -= 1;
right += 1;
}
else{ //down
left += 1;
right -= 1;
}
}
cout << left << "/" << right << endl;
return 0;
}