Code[vs] 1083 Cantor表
2015-08-05 18:28
435 查看
题目描述 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,…
![](http://codevs.cn/media/image/problem/1083.png)
输入描述 Input Description
整数N(1≤N≤10000000)
输出描述 Output Description
表中的第N项
样例输入 Sample Input
7
样例输出 Sample Output
1/4
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int N;
cin>>N;
long long Pai=int(sqrt(8*N+1)-1)/2;//算出是第几斜排
long long ShengYu=N-Pai*(Pai+1)/2;
int ant=ShengYu==0?Pai:Pai+1;
if(ShengYu==0)ShengYu=ant;
if(ant%2==0)
cout<<(1+ShengYu-1)<<"/"<<(ant-ShengYu+1)<<endl;
else
cout<<(ant-ShengYu+1)<<"/"<<(1+ShengYu-1)<<endl;
return 0;
}
现代数学的著名证明之一是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,…
![](http://codevs.cn/media/image/problem/1083.png)
输入描述 Input Description
整数N(1≤N≤10000000)
输出描述 Output Description
表中的第N项
样例输入 Sample Input
7
样例输出 Sample Output
1/4
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int N;
cin>>N;
long long Pai=int(sqrt(8*N+1)-1)/2;//算出是第几斜排
long long ShengYu=N-Pai*(Pai+1)/2;
int ant=ShengYu==0?Pai:Pai+1;
if(ShengYu==0)ShengYu=ant;
if(ant%2==0)
cout<<(1+ShengYu-1)<<"/"<<(ant-ShengYu+1)<<endl;
else
cout<<(ant-ShengYu+1)<<"/"<<(1+ShengYu-1)<<endl;
return 0;
}
相关文章推荐
- Hadoop集群(第4期)_SecureCRT使用
- 2015GitWebRTC编译实录17-audio_processing_neon编译问题解决
- Android 操作SQLite基本用法
- 用单向链表实现的一个管理系统
- UIScrollView(滑动切换图片,图片的循环切换(自动))
- Linux 网络堆栈的排队机制
- HDU1.3.5 Saving HDU
- SEO和SEM之间的区别和优劣势有哪些?
- Android Studio自动补全功能
- Unity 5.x AssetBundles
- 解决javah生成c头文件时找不到android类库的问题
- Eclipse集成Tomcat
- 回调函数
- 根据数据显示行的样式
- Hadoop集群(第3期)_VSFTP安装配置
- 在xcode上利用Mac来运行cocos2dx项目工程出现的问题
- 阿里云主机使用流程
- 解决javah生成c头文件时找不到android类库的问题
- 用命令行界面升级AXIS摄像头firmware
- dm8148 vpssM3 cameralink 源码解析