[洛谷]P1014 Cantor表 (#模拟 -1.17)(#数学 -1.9)

版权声明:JCBP工作室 & A.pro https://blog.csdn.net/Apro1066/article/details/82503123

题目描述

现代数学的著名证明之一是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,…

输入输出格式

输入格式:

整数N(1≤N≤10000000)

输出格式:

表中的第N项

输入输出样例

输入样例#1

7

输出样例#1

1/4

思路

先暴力搜索一下,本题数据很水,题目随便搞。

如果不知道数学定理就模拟,别想太多。

找规律

x/y 1 2 3 4 (y)
1 1/1 1/2 1/3 1/4 ...
2 2/1 2/2 2/3 2/4 ...
3 3/1 3/2 3/3 3/4 ...
4 4/1 4/2 4/3 4/4 ...
(x) ... ... ... ... ...

1.位于第x行第y列的数正是x/y。

2.当x==1时y+1,当y==1时x+1。

3.讨论x+y的奇偶,可以得出x+y为偶数,x=x-1,y=y+1;x+y为奇数,x=x+1,y=y-1。

#include <stdio.h>
#include <iostream>
using namespace std;
int x(1),y(1);//第一个数 
inline bool Iseven(int x1,int y1)//判断x+y是否为偶数 
{
	if((x1+y1)%2==0)
	{
		return 1;
	}
	return 0;
}
inline void search()//模拟x/y的下一个数 
{
	if(Iseven(x,y))
	{
		if(x==1)
		{
			y++;
		}
		else
		{
			x--;
			y++;
		}
	}
	else
	{
		if(y==1)
		{
			x++;
		}
		else
		{
			x++;
			y--;
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,i,s(0);
	cin>>n;
	for(i=1+1;i<=n;i++)//从第二个数开始搜 
	{
		search();
	}
	cout<<x<<'/'<<y<<endl;
	return 0;
}

这题用数学方法做我WA了一个点。如果有高手(dalao)的话欢迎评论。

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,i,s(0);
	cin>>n;
	for(i=0;s<=n;i++,s=s+i);
	if(i%2==1)
	{
		cout<<s-n+1<<'/'<<i+n-s<<endl;
		return 0;
	}
	cout<<i+n-s<<'/'<<s-n+1<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Apro1066/article/details/82503123
1.9