题目来源: 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. ICaptureGraphBuilder2::RenderStream 智能连接方法浅析

    ICaptureGraphBuilder2::RenderStream HRESULT RenderStream( [in] const GUID *pCategory, [in] const GUI ...

  2. etcd 命令行

    比较重要的配置 -name 节点名称,默认是UUID-data-dir 保存日志和快照的目录,默认为当前工作目录-addr 公布的ip地址和端口. 默认为127.0.0.1:2379-bind-add ...

  3. 73. Set Matrix Zeroes (Array)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...

  4. PEAR

    简介:pear是php扩展与应用库(the php extension and application repository)的缩写.它是一个php扩展及应用的一个代码仓库. 编码规范:参考(http ...

  5. MD5加密算法原理及其应用

    MD5是一个安全的散列算法,输入两个不同的明文不会得到相同的输出值,根据输出值,不能得到原始的明文,即其过程不可逆:所以要解密MD5没有现成的算法,只能用穷举法,把可能出现的明文,用MD5算法散列之后 ...

  6. SVG脚本编程简介

    本文主要介绍SVG的脚本编程,并分别给出放大.缩小,查询,鼠标事件等实例. 一. SVG简介 SVG,全称为Scalable Vector Graphics(可伸缩矢量图形).它是W3C制定的.用矢量 ...

  7. [Jmeter]如何才能通过ant运行jmeter

    在开始运行build.xml之前,还有一步必须要做,那就是将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到Ant所在目录lib子目录之下,这样Ant运行时才 ...

  8. Java程序设计16——Annotatio注释

    Annotation是代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行相应的处理.通过使用Annotation,程序开发人员可以在不改变原有逻辑的情况下,在源文件嵌入一些补充信息.代 ...

  9. Mac下eclipse 启动时出现An error has occurred. See the log file的问题

    eclipse原来可以使用的好好的,装了多个版本的jdk后,打开eclipse出现An error has occurred. See the log file的问题,经过查找,可能原因之一是机子装了 ...

  10. 看图说话:关于BI那点事儿

    [编者按]BI=DW+数据挖掘+业务分析+社会学?BI三部曲:管数据.看数据.源数据.BI有三种放法:技术部.业务部和独立部门.BI的工作=20%数据平台+30%数据支持+50%数据应用.