链接: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

输入

复制

7

输出

复制

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);

  


最新文章

  1. nginx 301 永久重定向
  2. How to reset password for unknow root
  3. [CS231n-CNN] Linear classification II, Higher-level representations, image features, Optimization, stochastic gradient descent
  4. eclipse在Ubuntu 13.04下的安装过程及问题小记
  5. 泌尿系统 Excretory system
  6. 下载个jquery-easyui-1.3.0使用,把他导入到myeclipse10里,jquery-1.7.2.min.js报错。 错误如下, Syntax error on token &quot;Invalid Regular Expression Options&quot;, no accurate correc
  7. React vs Angular 2: 冰与火之歌
  8. MySQL中char、varchar和text的区别
  9. XML巩固
  10. jquery数字验证大小比较
  11. Android GPS应用:动态获取位置信息
  12. HTTP could not register URL http://+:86/. 设置VS默认以管理员权限打开
  13. Python中的函数与变量
  14. spring-boot的spring-cache中的扩展redis缓存的ttl和key名
  15. VirtualBox下安装linux虚拟机
  16. git 琐碎
  17. exec函数族
  18. tensorflow example1
  19. 题解 P2580 【于是他错误的点名开始了】
  20. Xgboost理解

热门文章

  1. SQLAIchemy 学习(一)Session 相关
  2. 下载工具系列——Aria2 (几乎全能的下载神器)
  3. flutter RN taro选型思考
  4. Spring Boot Cache使用与整合
  5. 论文阅读: VITAMIN-E: Extremely Dense Feature Points
  6. 探索FFmpeg
  7. python 做词云图
  8. selenium登录爬取知乎出现:请求异常请升级客户端后重试的问题(用Python中的selenium接管chrome)
  9. http内网转发
  10. 配置 SSL、TLS 以及 HTTPS 来确保 Elasticsearch、Kibana、Beats 和 Logstash 的安全