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

题目描述

现代数学的著名证明之一是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/y1234(y)
11/11/21/31/4...
22/12/22/32/4...
33/13/23/33/4...
44/14/24/34/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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值