题目链接:1083 Cantor表
题目:
题目描述 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
解题思路:
模拟。
注意点:
1.审题要仔细。(我刚开始以为只需要对有理数进行编号,结果是对全部的数都要编号),浪费了我的一些时间。
AC代码:
#include
#include
using namespace std;
int main(void) {
int a, b;
int n;
scanf("%d", &n);
int ans = 1; a = b = 1;
while(ans < n) {
if(a == 1 && (b & 1)) {
b ++;
ans ++;
} else if(a == 1) {
while(b != 1) {
b--; a ++;
ans ++;
if(ans == n) break;
}
} else if(b == 1 && (a & 1) == 0) {
a ++; ans ++;
} else if(b == 1) {
while(a != 1) {
a --; b ++;
ans ++;
if(ans == n) break;
}
}
}
printf("%d/%d\n", a, b);
return 0;
}