Fork me on GitHub

Uva 729 - The Hamming Distance Problem

 The Hamming Distance Problem 

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=670

今天做的这三题都差不多,还是位运算的问题呵!

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8 
 9     int T = 0, list[20];
10     cin>>T;
11     for(int k=1; k<=T; ++k)
12     {
13         int n, h;
14         cin>>n>>h;
15         for(int i=0; i<(1<<n); ++i)
16         {
17             int num = 0;
18             int cnt = 0;
19             for(int j=0; j<n; ++j)
20             if(i&(1<<j)) num++;
21             if(num == h)
22             {
23                 int temp = i;
24                 int list[20];
25                 memset(list, 0, sizeof(list));
26                 for(int t=1; t<=n; ++t)
27                 {
28                     list[t-1] = temp%2;
29                     temp = temp/2;
30                 }
31                 for(int t=n; t>=1; --t)
32                     cout<<list[t-1];
33                 cout<<endl;
34             }
35         }
36         if(k != T) cout<<endl;
37     }
38     return 0;
39 }

 

posted @ 2013-04-16 13:59  Gifur  阅读(275)  评论(0)    收藏  举报
TOP