欠下好多java web和android的内容没记录,先整理一下这两周看的东西吧。T-T
说一下二进制枚举
看两个题
#include<bits/stdc++.h> using namespace std; int main() { int n,x,countt=0; cin>>n>>x; int arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } for(int i=0;i<(1<<n);i++) { int sum=0; for(int j=0;j<n;j++) { if(i&(1<<j)) { sum+=arr[j]; } } if(sum==x) { countt++; } } cout<<countt<<endl; return 0; }
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int m,n,k; 6 cin>>n>>m>>k; 7 int arr[n][15]; 8 for(int i=0;i<n;i++) 9 { 10 cin>>arr[i][0]; 11 for(int j=1;j<=arr[i][0];j++) 12 { 13 cin>>arr[i][j]; 14 } 15 } 16 17 int maxx=0; 18 for(int i=0;i<(1<<k);i++)//在k种玩具中选取 19 { 20 //已购买数目 21 int choose=0; 22 //满足的学生数目 23 int satisfy=0; 24 //某个玩具是否已经购买 25 int mark[i+1]; 26 memset(mark,0,sizeof(mark)); 27 for(int j=0;j<k;j++) 28 { 29 if(i&(1<<j)) 30 { 31 choose++; 32 mark[j+1]=1; 33 } 34 } 35 if(choose==m) 36 { 37 for(int x=0;x<n;x++)//遍历n个学生的需求 38 { 39 if(arr[x][0]==0) 40 { 41 satisfy++; 42 continue; 43 } 44 for(int y=1;y<=arr[x][0];y++) 45 { 46 if(!mark[arr[x][y]]) 47 { 48 break; 49 } 50 if(y==arr[x][0])//满足第x+1个学生的所有需求 51 { 52 satisfy++; 53 } 54 } 55 } 56 57 if(maxx<satisfy) 58 { 59 maxx=satisfy; 60 } 61 } 62 } 63 cout<<maxx<<endl; 64 return 0; 65 }
原文:http://www.cnblogs.com/wangkaipeng/p/6410940.html