1083 Cantor表

1083 Cantor表

 

1999年NOIP全国联赛普及组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 白银 Silver
 
 
 
题目描述 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

数据范围及提示 Data Size & Hint

见描述

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 
 6 int main(int argc, char** argv) 
 7 {
 8     int N, k, i;
 9     cin>>N;
10     if(N == 1)
11         cout<<"1/1"<<endl;
12     else
13     {
14         k = (int)floor((sqrt(1+8*N)-1)/2-1e-9)+1;
15         i = N - k*(k-1)/2;
16         if(k%2 == 1)
17             cout<<k-i+1<<'/'<<i<<endl;
18         else
19             cout<<i<<'/'<<k-i+1<<endl;    
20     }
21     return 0;
22 }
View Code

难点在于行号K的确定,其余都很简单!即如何用编程表示(sqrt(1+8*N)-1)/2<<k<(sqrt(1+8*N)+1)/2??思考许久方得良方~

posted @ 2014-09-14 10:50  名草震江湖  阅读(335)  评论(0编辑  收藏  举报