Cantor表(模拟)

链接:https://ac.nowcoder.com/acm/contest/1069/I
来源:牛客网

题目描述

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

 我们以Z字形给上表的每一项编号。第一项是1/1,然后是1/2,2/1,3/1,2/2,…


输入描述:

整数N(1≤N≤10000000)

输出描述:

表中的第N项

输入

 7

输出

1/4

简单模拟就行了

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <algorithm>
 6 #include <queue>
 7 const int INF=0x3f3f3f3f;
 8 using namespace std;
 9 
10 int main()
11 {
12     int n;
13     scanf("%d",&n);
14     int t;
15     for(int i=1;i<=n;i++)
16     {
17         if(i*(i+1)/2>=n)
18         {
19             t=i-1;
20             break;
21         }
22     }
23     int tt=n-(t*(t+1)/2);
24     if((t+1)%2)
25     {
26         printf("%d/%d\n",t+1-tt+1,tt);
27     }
28     else if((t+1)%2==0)
29         printf("%d/%d\n",tt,t+1-tt+1);
30     return 0;
31 }
 

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11261754.html