51nod 1087 1 10 100 1000
2017-03-28 11:26
357 查看
思路1 打表+二分查找
思路2 直接公式推导
因为上午才看了个打表的,所以这次试一下打表 效率肯定是公式推导比较高,等差数列嘛
思路2 直接公式推导
因为上午才看了个打表的,所以这次试一下打表 效率肯定是公式推导比较高,等差数列嘛
//@auther zhou //@Number 201408070203 //@start time: //@finish time: /*@此处注意: */ /* 测试数据 */ #include<iostream> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #include<windows.h> using namespace std; long long a[50000]; void init(){ for(long long i=0;i<50000;i++){ a[i]=(i*(i+1))/2+1; } } int find(long long x){//妈的二分法都写不好。。 int index=25000,left=0,right=50000; int num=0; while(1){ if(left==right-1||left==right+1||a[index]==x){ if(a[index]==x) { // cout<<"aindex="<<a[index]<<"匹配"<<endl; return 1; } else { // cout<<"a[index]不匹配:"<<a[index]<<endl; return 0; } } if(a[index]>x){// right=index; index=(index+left)/2; // cout<<"要找的在左边 index=" <<index<<"left ,right="<<left<<"<<<<"<<right<<"aleft="<<a[left]<<"aright="<<a[right]<<endl; } if(a[index]<x){ left=index; index=(index+right)/2; // cout<<"要找的在右边 index=" <<index<<"left ,right="<<left<<"<<<<< "<<right<<"aleft="<<a[left]<<"aright="<<a[right]<<endl; } // cout<<"次数"<<num++<<endl; // Sleep(500); } } int find2(long long x){//妈的二分法都写不好。。第二版 int index=25000,left=0,right=50000; //int num=0; while(left<=right){ index=(left+right)/2; if(x==a[index]) return 1; if(a[index]>x){// right=index-1; } if(a[index]<x){ left=index+1; } } return 0; } int main(){ long long num,temp; cin>>num; init(); // for(int i=0;i<100;i++) // cout<<a[i]<<endl; //cout<<a[49999]/1000000000; for(long long i=0;i<num;i++){ cin>>temp; if(find2(temp)) cout<<1<<endl; else cout<<0<<endl; } return 0; }
相关文章推荐
- 51Nod 1087 1 10 100 1000
- 51nod 1087 1 10 100 1000
- 51Nod 1087 1 10 100 1000
- 51nod-1087 . 1 10 100 1000
- 51nod 1087 1 10 100 1000【递推】
- 51nod 1087 1 10 100 1000
- 【51Nod】1087 - 1 10 100 1000 问题(水)
- 51nod 1087 1 10 100 1000
- 51nod 1087 1 10 100 1000
- 51Nod 1087 1 10 100 1000
- 51Nod-1087-1 10 100 1000
- 【51Nod】1087 1 10 100 1000
- 51Nod 1087 1 10 100 1000 | 数学
- 二分51nod-1087 1 10 100 1000
- 51nod--1087 1 10 100 1000
- 51nod 1087 1 10 100 1000
- 51nod-1087 . 1 10 100 1000
- 51nod 1087《1 10 100 1000》
- 51nod 1087 1 10 100 1000
- 51Nod-1087 1 10 100 1000【模拟】