zoukankan      html  css  js  c++  java
  • 51nod 1087 1 10 100 1000(找规律+递推+stl)

    题目来源: 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

    观察到第一个1在第一位,第二个1在第1+1位,第三个1在(1+1)+2位,第n个1在(第n-1个的位数)+n-1.
    用ans数组存放为1的位,然后看n是否在ans数组中。

     1 #include <iostream>
     2 using namespace std;
     3 const int maxn=1e6+10;
     4 int n;
     5 long long ans[maxn]={0};
     6 int len;
     7 void init()
     8 {
     9     len=0;
    10     ans[0]=0;
    11     ans[1]=1;
    12     for(int i=2;i<maxn;i++)
    13     {
    14         ans[i]=ans[i-1]+i-1;
    15     }
    16 }
    17 int main()
    18 {
    19     int t;
    20     cin>>t;
    21     init();
    22     while(t--)
    23     {
    24         cin>>n;
    25         long long pos=lower_bound(ans,ans+maxn,n)-ans;
    26         if(ans[pos]==n)
    27             cout<<1<<endl;
    28         else
    29             cout<<0<<endl;
    30     }
    31     return 0;
    32 }
    View Code
     
  • 相关阅读:
    Node.js 常用工具 util
    jQuery 选择器
    Node.js 创建HTTP服务器
    Node.js GET/POST请求
    JavaScript 用法
    Node.js 事件
    Node.js 函数
    Bootstrap<基础二> 网格系统
    读文章《Flexbox详解》笔记
    好文要读
  • 原文地址:https://www.cnblogs.com/onlyli/p/7253987.html
Copyright © 2011-2022 走看看