题目描述
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。
Sample Input
3
1
2
3
Sample Output
1
1
0
题目分析
先预处理出所有1的位置,用map存下来。然后查询即可。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=1e5+5;
unordered_map<int,int> ma; //unordered_map的查询效率更高
int main()
{
for(int i=1,j=1;i<=1e9+5;j++) //预处理出1的位置
{
ma[i]++;
i+=j;
}
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<ma[n]<<endl; //输出查询结果
}
return 0;
}