sicily 1122 Prerequisites?

solution 1:

 

// source code of submission 765531, Zhongshan University Online Judge System #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int k,m,i,j,cour_num,flag; //cour_num为已选的课程编号 int cate_num,cate_mst,cnt; //cate_num是某类课程包含的科目数,cate_mst是该类课程必修的科目数 while(cin>>k,k) { cin>>m; flag = 1; vector<int> v; for(i = 0;i < k;i++) cin>>cour_num,v.push_back(cour_num); while(m--) { cin>>cate_num>>cate_mst; int *a = new int[100]; for(i = 0;i < cate_num;i++) cin>>a[i]; cnt = 0; vector<int>::iterator iter; for(j = 0;j < cate_num;j++) { iter = find(v.begin(),v.end(),a[j]); if(iter != v.end()) cnt++; } if(cnt<cate_mst) { flag = 0; } } if(flag == 1) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }

 

solution 2:

 

// source code of submission 765533, Zhongshan University Online Judge System #include <iostream> #include <cstring> #include <algorithm> using namespace std; int b_search(int *a,int n,int value) { int low = 0,high = n-1; while(low <= high) { int mid = low + (high-low)/2; if(a[mid] == value) return 1; if(a[mid] < value) low = mid+1; else high = mid-1; } return -1; } int main() { int k,m,i,j,flag; int cate_num,cate_mst,cnt; //cate_num是某类课程包含的科目数,cate_mst是该类课程必修的科目数 int c[106],a[106]; while(cin>>k,k) { cin>>m; flag = 1; memset(c,0,sizeof(c)); for(i = 0;i < k;i++) cin>>c[i]; while(m--) { cin>>cate_num>>cate_mst; memset(a,0,sizeof(a)); for(i = 0;i < cate_num;i++) cin>>a[i]; cnt = 0; sort(c,c+k); //二分查找一定要先排序 for(j = 0;j < cate_num;j++) if(b_search(c,k,a[j])==1) cnt++; if(cnt < cate_mst) flag = 0; } if(flag == 1) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }

你可能感兴趣的:(c,search,iterator)