题解 P1014 【Cantor表】
jianzihao
2018-11-23 21:47:14
### ~~话说回来我今晚吃屎了吧一道水题提交了三次还没accepted~~
# 这道题呢并不难,只要你会找规律就行:
Somebody says that we should to define an array,but I think it needn't.
## 规律:
```cpp
分子:1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4
5 5
6
分母:
简单粗暴:分母=行数-分子+1
```
## 源码刨析:
第一段:
```
#include<bits/stdc++.h>
using namespace std;//基本定义,预编译
```
第二段:
```
int main()
{
int n,xs,hs,ls;
scanf("%d",&n);//基本的读入
for(xs=1;xs*(xs+1)/2<n;xs++);
//这行比较关键,xs是cantor表的斜行数(原理参照三角形数)
```
什么?你连[三角形数](https://baike.baidu.com/item/三角形数/5836794?fr=aladdin)都不知道?
第三段:
```
hs=xs-(xs*(xs+1)/2-n);
ls=xs-hs+1;
if(xs%2==0)
{
printf("%d",hs);
printf("/");
printf("%d",ls);
}
else
{
printf("%d",ls);
printf("/");
printf("%d",hs);
}
```
这是整个程序的精华,正是因为它,程序才能正常运行,并输出结果。
第四段:结尾,完成
```
return 0;
}
```
最后,做题时切记不要心慌,冷静分析。