前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1083 Cantor表

1083 Cantor表

作者头像
attack
发布2018-04-11 11:58:15
5190
发布2018-04-11 11:58:15
举报
文章被收录于专栏:数据结构与算法

题目描述 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

见描述

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 int tot=1;
 7 int ans=2;
 8 int now=1;// 1向下 2向上 
 9 int a[101][101];
10 int main()
11 {
12     int n;
13     cin>>n;
14     int i=1,j=1;
15 //    cout<<1<<" ";
16     while(tot<n)
17     {
18         if(now==1)
19         {
20             tot++;
21             if(j==1)
22             {
23                 if(j==1)
24                 i++;
25                 else j++;
26                 now=2;
27                 continue;
28             }
29             else
30             {
31                 i++;
32                 j--;
33             }
34             
35         }
36         if(now==2)
37         {
38             tot++;
39             if(i==1)
40             {
41                 if(i==1)
42                 j++;
43                 else i++;
44                 now=1;
45                 continue;
46             }
47             else
48             {
49                 i--;
50                 j++;    
51             }
52         }
53         else now=1;    
54     }
55     cout<<j<<"/"<<i;
56     return 0;
57 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档