题目来源: Ural 1209
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
1,10,100,1000...组成序列1101001000...,求这个序列的第N位是0还是1。

 
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)
Output
共T行,如果该位是0,输出0,如果该位是1,输出1。
Input示例
3
1
2
3
Output示例
1
1
0

刚开始想用打表的方法,但是没有写出来,后来看了别人代码,发现原来有数学规律,也有人用set来做

数学规律

其实是有规律的

1 = 1

2 = 1 + (1)

4 = 1 + (1+2)

7 = 1 + (1+2+3)

.....

即 X*(X-1)/2  + 1 == n有解

另t = (int)sqrt(2*n-2)  t*(t+1)==2*(n-1)成立时有解

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std; int main()
{
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
int t = (int)sqrt(2*(n-1));
if (t*(t+1) == 2*(n-1))
printf("1\n");
else
printf("0\n");
} return 0;
}

  

set

#include <bits/stdc++.h>
#define N 1000000000
using namespace std;
set<int> s;
int init(){
s.insert(1);
int ans=1;
for(int i=1;ans+i<=1000000000;i++){
s.insert(ans+i);
ans+=i;
}
}
int main(){
int n;
scanf("%d",&n);
init();
while(n--){
int m;
scanf("%d",&m);
if(s.count(m))
printf("1\n");
else
printf("0\n");
}
return 0;
}

  

打表  二分

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std; const int maxn = 100100;
int One[maxn]; void preOne()
{
int i = 1;
One[0] = 1;
while (i < maxn) {
One[i] = One[i-1]+i;
i++;
} } bool Find(int x)
{
int l = 0, r = maxn-1, mid;
while (l <= r) {
mid = (l+r)>>1;
if (One[mid] > x)
r = mid-1;
else if (One[mid] < x)
l = mid+1;
else return true;
}
return false;
} int main()
{
//freopen("1.txt", "r", stdin);
preOne();
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
if (Find(n))
printf("1\n");
else
printf("0\n");
} return 0;
}

  

0

1087 1 10 100 1000(打表 set 数学)的更多相关文章

  1. 51nod 1087 1 10 100 1000【打表】

    题目来源: Ural 1209 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 1,10,100,1000...组成序列1101001000...,求 ...

  2. 51NOD 1087 1 10 100 1000

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1087 暴力大法 #include<bits/stdc++.h> ...

  3. 51Nod 1087 1 10 100 1000 | 数学

    Input示例 3 1 2 3 Output示例 1 1 0 #include "bits/stdc++.h" using namespace std; #define LL lo ...

  4. Ural 1209. 1, 10, 100, 1000... 一道有趣的题

    1209. 1, 10, 100, 1000... Time limit: 1.0 secondMemory limit: 64 MB Let's consider an infinite seque ...

  5. Timus - 1209 - 1, 10, 100, 1000...

    先上题目: 1209. 1, 10, 100, 1000... Time limit: 1.0 secondMemory limit: 64 MB Let's consider an infinite ...

  6. [51NOD1087]1 10 100 1000(规律,二分)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1087 用高中的数列知识就可以推出公式,不难发现f(n)=f(n ...

  7. 背水一战 Windows 10 (100) - 应用间通信: 分享

    [源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...

  8. 用js实现随机选取10–100之间的10个数字,存入一个数组,并排序

    var iArray = []; function getRandom(istart, iend) { var iChoice = iend - istart + 1; //加1是为了取到100 va ...

  9. python 统计MySQL大于100万的表

    一.需求分析 线上的MySQL服务器,最近有很多慢查询.需要统计出行数大于100万的表,进行统一优化. 需要筛选出符合条件的表,统计到excel中,格式如下: 库名 表名 行数 db1 users 1 ...

随机推荐

  1. 28.OGNL与ValueStack(VS)-总结$ # %的区别

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html $用于i18n和struts配置文件 #取得ActionContext的值 ...

  2. RAD XE8

    http://community.embarcadero.com/index.php/blogs/entry/rad-studio-2015-roadmap http://www.embarcader ...

  3. liquibase使用

    1. 创建表 drop database if exists mybatis; create database mybatis; use mybatis; create table mybatis.C ...

  4. java 重定向和转发 的区别

    重定向方式的含义是第一个页面通知浏览器发送一个新的页面请求.因为,当你使用重定向时,浏览器中所显示的URL会变成新页面的URL, 而当使用转发时,该URL会保持不变. 重定向的速度比转发慢,因为浏览器 ...

  5. 使用Visual Studio进行 Android开发的十大理由

    [原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...

  6. Selenium+TestNG+Maven 搭建

    Java环境配置 Eclipse配置TestNG Eclipse配置Maven pom.xml文件相关配置,添加依赖selenium和TestNg的jar包 <!-- https://mvnre ...

  7. CentOS错误

    centos下yum lock的解决办法 Another app is currently holding the yum lock; waiting for it to exit... 解决办法:  ...

  8. What's App has the Qt?

    收集了我看到的使用Qt开发的应用程序或者含有Qt库的应用程序 CNTV CNTV, 一个中央电视台的视频直播软件, 从下面卸载后的残余目录树,可以看到,存在部分库使用的就是Qt的.下面的目录树,已经删 ...

  9. NoSQL数据库笔谈

    NoSQL数据库笔谈 databases , appdir , node , paper颜开 , v0.2 , 2010.2 序 思想篇 CAP 最终一致性 变体 BASE 其他 I/O的五分钟法则 ...

  10. PHPStorm 忽略 node_modules 目录

    如果项目中包含 node_modules 目录,会使 PHPStorm 卡得很慢, 原因:PHPStorm 在进行大量的扫描工作. 解决:忽略它 原文地址:https://segmentfault.c ...