51nod 1010 只包含因子2 3 5的数 【容器 +二分 】
2017-10-22 20:21
417 查看
这道题 看了网上全是 一种解法,就没再看,想自己用容器模拟出来, 可是没想到 自己用的不是很熟练, 有思路,但写不出来,因此 ,我又借鉴了 另一位高人的博客,(这里 入门经典里面也有 这种类似题 方法很不错 )
下面是普遍使用的方法
4000
/* 这是我写的 但是出粗了 哎 ,心里很不是滋味! 不过也增加了一些熟练度,互勉 #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<set> using namespace std; int a[10000]; int main(){ long long t; cin>>t; long long maxnum=0; for(int i=0;i<t;i++){ cin>>a[i]; if(a[i]>maxnum) maxnum=a[i]; } vector<long long> q; set<long long > buf; long long u=0,o=1,p=2; q.push_back(2); q.push_back(3); q.push_back(5); for(long long i=0;i<maxnum;i++){ long long temp=min(q[u],min(q[o],q[p])); buf.insert(temp); if(temp>maxnum) break; if(q[u]==temp) u+=3; if(q[o]==temp) o+=3; if(q[p]==temp) p+=3; } for(int i=0;i<t;i++) cout<<*buf.lower_bound(i)<<endl; //set不可使用 这个函数 return 0; } */ 大牛使用的是 不定长数组 (基本过程 是 从小的算到大 将小的 存入另一个vector中 最后进行 查询的操作 #include <iostream> #include <algorithm> #include <string.h> #include <vector> using namespace std; vector<long long int> buf; long long input[10000]; long long int find(int size, long long int target){ long long int left = 0; long long int right = size-1; while (left < right-1){ long long int mid = left + (right-left)/2; if (buf[mid] < target) left = mid; else right = mid; } if (buf[left] >= target) return buf[left]; return buf[right]; } int main(){ int T; cin >> T; long long int maxNum = 0; for (int i = 0; i < T; i++){ cin >> input[i]; if (input[i] > maxNum) maxNum = input[i]; } vector<long long int> a; int p = 0; int q = 1; int r = 2; a.push_back(2); a.push_back(3); a.push_back(5); for (long long int i = 0; i < maxNum; i++){ long long int temp = min(a[p], min(a[q], a[r])); buf.push_back(temp); if (temp > maxNum) break; a.push_back(temp*2); a.push_back(temp*3); a.push_back(temp*5); if (a[p] == temp) p += 3; if (a[q] == temp) q += 3; if (a[r] == temp) r += 3; } int size = buf.size(); for (int i =0; i < T; i++){ long long int result = find(size, input[i]); cout << result << endl; } return 0; }
下面是普遍使用的方法
#include <iostream> #include <algorithm> #include <map> using namespace std; typedef long long ll; const ll maxn=1e18+100; ll a[100000]; int k; void init()//打表记录因子中只包含2,3,5的数 { ll i,j,z; for(i=1;i<maxn;i=i*2){//只需i每次倍增,不需每次都++ for(j=1;j*i<maxn;j=j*3){ for(z=1;i*j*z<maxn;z=z*5){ a[k++]=i*j*z; } } } } int main() { init(); sort(a,a+k); int t; ll n,ans; cin>>t; while(t--) { cin>>n; cout<<*lower_bound(a+1,a+k,n)<<endl; } return 0; }
4000
相关文章推荐
- 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
- 【51nod】--1010 只包含因子2 3 5的数 (二分&&数论&&打表)
- 51nod 1010 只包含因子2 3 5的数【打表+二分】
- 51nod 1010 只包含因子2 3 5的数(预处理+二分)
- 51nod 1010 只包含因子2 3 5的数 打表二分
- 51Nod 1010 只包含因子2 3 5的数(打表+二分)
- 51NOD 1010 只包含因子2 3 5的数(二分 + 预处理)
- 51nod 1010 只包含因子2 3 5的数 (打表+二分)
- 51Nod 1010 只包含因子2 3 5的数 打表+二分
- 51Nod-1010 只包含因子2 3 5的数【打表+排序+二分搜索】
- 51nod 1010 只包含因子2 3 5的数
- 51nod-1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数 打表
- 51nod 1010 只包含因子2 3 5的数
- 51Nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51Nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod-1010 只包含因子2 3 5的数