Cantor表-(模拟)
2024-09-17 16:48:21
链接:https://ac.nowcoder.com/acm/contest/1069/I
来源:牛客网
题目描述
现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的:
我们以Z字形给上表的每一项编号。第一项是1/1,然后是1/2,2/1,3/1,2/2,…
输入描述:
整数N(1≤N≤10000000)
输出描述:
表中的第N项
示例1
输出
1/4 题解:斜着看,每一斜逐渐增加1个数,奇数斜从下往上,偶数斜从上往下,暴力求包括到第几斜,从新的一斜开始。分奇数偶数情况讨论。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int a[]; int main()
{
memset(a,,sizeof(a));
for(int i=;i<=;i++)
a[i]=a[i-]+i;
int n;
scanf("%d",&n);
int l,cha;///第几斜前是铺满的,新的一斜有几个数
for(int i=;i<=;i++)
{
if(a[i]<=n && n<=a[i+])
{
l=i;
cha=n-a[i];
break;
}
}
int x,y;
if(l%)///第奇数斜,轮到第偶数斜,右上→左下
{
if(cha==)
x=,y=l;
else
{
x=;
y=l+;
cha--;
while(cha--)
{
x++;
y--;
}
}
}
else
{
if(cha==)
x=l,y=;
else
{
x=l+;
y=;
cha--;
while(cha--)
{
x--;
y++;
}
}
}
printf("%d/%d\n",x,y);
return ;
}
c++版本
水题,拿来练习matlab。
a(1)=1;
for i=2:1:10000
a(i)=a(i-1)+i;
end
n=input('');
l=0;
cha=0;
x=0;
y=0;
for i=1:1:10000
if a(i)<=n && n<=a(i+1)
l=i;
cha=n-a(i);
break;
end
end
if mod(l,2)==1 %matlab求模
if cha==0
x=1;
y=l;
else
x=1;
y=1+l;
cha=cha-1;
while cha>0
x=x+1;
y=y-1;
cha=cha-1;
end
end
else
if cha==0
x=l;
y=1;
else
x=l+1;
y=1;
cha=cha-1;
while cha>0
x=x-1;
y=y+1;
cha=cha-1;
end
end
end
fprintf('%d/%d\n',x,y);
最新文章
- nginx 301 永久重定向
- How to reset password for unknow root
- [CS231n-CNN] Linear classification II, Higher-level representations, image features, Optimization, stochastic gradient descent
- eclipse在Ubuntu 13.04下的安装过程及问题小记
- 泌尿系统 Excretory system
- 下载个jquery-easyui-1.3.0使用,把他导入到myeclipse10里,jquery-1.7.2.min.js报错。 错误如下, Syntax error on token ";Invalid Regular Expression Options";, no accurate correc
- React vs Angular 2: 冰与火之歌
- MySQL中char、varchar和text的区别
- XML巩固
- jquery数字验证大小比较
- Android GPS应用:动态获取位置信息
- HTTP could not register URL http://+:86/. 设置VS默认以管理员权限打开
- Python中的函数与变量
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
- VirtualBox下安装linux虚拟机
- git 琐碎
- exec函数族
- tensorflow example1
- 题解 P2580 【于是他错误的点名开始了】
- Xgboost理解
热门文章
- SQLAIchemy 学习(一)Session 相关
- 下载工具系列——Aria2 (几乎全能的下载神器)
- flutter RN taro选型思考
- Spring Boot Cache使用与整合
- 论文阅读: VITAMIN-E: Extremely Dense Feature Points
- 探索FFmpeg
- python 做词云图
- selenium登录爬取知乎出现:请求异常请升级客户端后重试的问题(用Python中的selenium接管chrome)
- http内网转发
- 配置 SSL、TLS 以及 HTTPS 来确保 Elasticsearch、Kibana、Beats 和 Logstash 的安全