51nod 1087 1 10 100 1000
生活随笔
收集整理的這篇文章主要介紹了
51nod 1087 1 10 100 1000
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,10,100,1000...組成序列1101001000...,求這個序列的第N位是0還是1。 ?收起
找出所有的1的位置,然后二分查找。
代碼: #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath>using namespace std; int ans[50000],c; int check(int n) {int l = 0,r = c - 1;while(l < r) {int mid = (l + r + 1) / 2;if(ans[mid] > n) r = mid - 1;else l = mid;}return ans[l] == n ? 1 : 0; } int main() {for(int i = 1,j = 1;i <= 1000000000 - j;j += i ++) {ans[c ++] = j;}int t,n;scanf("%d",&t);for(int i = 0;i < t;i ++) {scanf("%d",&n);printf("%d\n",check(n));} }
輸入
第1行:一個數(shù)T,表示后面用作輸入測試的數(shù)的數(shù)量。(1 <= T <= 10000) 第2 - T + 1行:每行1個數(shù)N。(1 <= N <= 10^9)輸出
共T行,如果該位是0,輸出0,如果該位是1,輸出1。輸入樣例
3 1 2 3輸出樣例
1 1 0找出所有的1的位置,然后二分查找。
代碼: #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath>using namespace std; int ans[50000],c; int check(int n) {int l = 0,r = c - 1;while(l < r) {int mid = (l + r + 1) / 2;if(ans[mid] > n) r = mid - 1;else l = mid;}return ans[l] == n ? 1 : 0; } int main() {for(int i = 1,j = 1;i <= 1000000000 - j;j += i ++) {ans[c ++] = j;}int t,n;scanf("%d",&t);for(int i = 0;i < t;i ++) {scanf("%d",&n);printf("%d\n",check(n));} }
?
轉載于:https://www.cnblogs.com/8023spz/p/10872779.html
總結
以上是生活随笔為你收集整理的51nod 1087 1 10 100 1000的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微众银行we2000备用金怎么提现
- 下一篇: 前端相关的一些小工具