1087 1 10 100 1000(打表 set 数学)
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)
共T行,如果该位是0,输出0,如果该位是1,输出1。
3
1
2
3
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;
}
#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;
}
1087 1 10 100 1000(打表 set 数学)的更多相关文章
- 51nod 1087 1 10 100 1000【打表】
题目来源: Ural 1209 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 1,10,100,1000...组成序列1101001000...,求 ...
- 51NOD 1087 1 10 100 1000
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1087 暴力大法 #include<bits/stdc++.h> ...
- 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 ...
- 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 ...
- Timus - 1209 - 1, 10, 100, 1000...
先上题目: 1209. 1, 10, 100, 1000... Time limit: 1.0 secondMemory limit: 64 MB Let's consider an infinite ...
- [51NOD1087]1 10 100 1000(规律,二分)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1087 用高中的数列知识就可以推出公式,不难发现f(n)=f(n ...
- 背水一战 Windows 10 (100) - 应用间通信: 分享
[源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...
- 用js实现随机选取10–100之间的10个数字,存入一个数组,并排序
var iArray = []; function getRandom(istart, iend) { var iChoice = iend - istart + 1; //加1是为了取到100 va ...
- python 统计MySQL大于100万的表
一.需求分析 线上的MySQL服务器,最近有很多慢查询.需要统计出行数大于100万的表,进行统一优化. 需要筛选出符合条件的表,统计到excel中,格式如下: 库名 表名 行数 db1 users 1 ...
随机推荐
- redis一主二从三哨兵
redis做集群的时候有很多种配置方法,一主二从三哨兵这种模式是官网推荐的.,写配置文件链接的时候,写的是哨兵地址,不是IP,用户名,密码之类的. 一主二从很好理解,一个主的redis,实时备份到两个 ...
- 在Linux下删除文件及文件夹(rm)
删除目录.文件 rm(remove) 功能说明:删除文件或目录.语 法:rm [-dfirv][--help][--version][文件或目录...]补充说明:执行rm指令可删除文件或目录,如欲删除 ...
- Qt Image Water Marker
QString str = "input.jpg"; if(!img.load(str)){ return; } QImage mark(img.width()/2,img.hei ...
- mybatis总结回顾
1.mybatis的介绍 轻量级数据持久层框架,替代hibernate 2.mybatis的入门 导包-->配置文件(类名.xml.SqlMapConfig.xml) 类名.xml:放映射.sq ...
- oracle存储过程和游标参考
oracle open cursor forhttp://www.itpub.net/thread-1874683-1-1.html
- word生成包含echarts图形使用phantomjs,支持word2003,2007
代码具体地址在我的github上 :https://github.com/HaoTwoDonkey/QuickOutPutWord 写得不好,欢迎提出问题 下面说几个phantomjs使用过程中遇到的 ...
- 友盟统计小白教程:创建应用,申请appkey
上回书讲到,我们已经和一个靠谱的人选择一个靠谱的统计平台注册了一个帐号,下面就该创建一个应用了. 介绍一个基础知识: appkey:友盟识别app的唯一标识,目前友盟平台上超过500000款App,每 ...
- Oracle学习笔记(三)
五.操作表 1.表分为行和列 约定:每行数据唯一性,每列数据同类性,每列列名唯一性. 2.数据类型 字符型 -- 固定长度的字符类型 字符类型:CHAR(n)(MAX n=2000).NCHAR(MA ...
- linux平台使用spark-submit以cluster模式提交spark应用到standalone集群
shell脚本如下 sparkHome=/home/spark/spark-2.2.0-bin-hadoop2.7 $sparkHome/bin/spark-submit \ --class stre ...
- tomcat服务器输入localhost可以访问,ip无法访问解决办法
最近在开发项目中,遇到的一个问题是: 在 tomcat中发布一个web项目,但是发布成功后,只能用http://localhost:8080/fm访问项目,不能用 http://127.0.0.1:8 ...