zoukankan      html  css  js  c++  java
  • 1087 1 10 100 1000(打表 set 数学)

    题目来源: 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
    ");
            else
                printf("0
    ");
        }
    
        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
    ");
        else
          printf("0
    ");
      }
      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
    ");
            else
                printf("0
    ");
        }
    
        return 0;
    }
    

      

    0
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
    Spring data jpa 使用技巧记录
    Hibernate 关于实体映射常用注解
    Mysql数据库实用语句集
    免配置环境变量使用Tomcat+设置项目主页路径为http://localhost:8080+修改tomcat端口号
    Springboot+shiro配置笔记+错误小结
    python阳历转农历
    Aria2+WebUI+caddy搭建私有网盘
    java运算符优先级
    IntelliJ IDEA 快捷键
  • 原文地址:https://www.cnblogs.com/h-hkai/p/7479727.html
Copyright © 2011-2022 走看看