传说中的水题?我觉得还是有点考输出格式,提交了好几次,才通过,还好还好!呵呵。
/* ID: qq104801 LANG: C++ TASK: hamming */ #include <iostream> #include <fstream> #include <string> #include <vector> #include <cstdio> using namespace std; int n,b,d; vector<int> result; int Power(int x) { if(x==1)return 2; return 2*Power(x-1); } int check(int i) { for(int k=0;k<result.size();k++) { int dis=0; int c=i^result[k]; while(c) { dis+=c & 0x01; c >>= 1; } if(dis<d)return false; } return true; } void test() { freopen("hamming.in","r",stdin); freopen("hamming.out","w",stdout); cin >> n >> b >> d; result.push_back(0); for(int i=1;i<=Power(b)-1;i++) { if(result.size()<n) { if(check(i))result.push_back(i); } else break; } vector<int>::iterator it; int hh=0; for(it=result.begin();it<result.end();it++) { hh++; if(hh%10==1)cout<<*it; else if (hh%10==0)cout<<" "<<*it<<endl; else cout<<" "<<*it; } if(hh%10!=0) cout<<endl; } int main () { test(); return 0; }
test data:
USER: cn tom [qq104801] TASK: hamming LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.003 secs, 3508 KB] Test 2: TEST OK [0.003 secs, 3508 KB] Test 3: TEST OK [0.003 secs, 3508 KB] Test 4: TEST OK [0.003 secs, 3508 KB] Test 5: TEST OK [0.003 secs, 3508 KB] Test 6: TEST OK [0.003 secs, 3508 KB] Test 7: TEST OK [0.003 secs, 3508 KB] Test 8: TEST OK [0.003 secs, 3508 KB] Test 9: TEST OK [0.003 secs, 3508 KB] Test 10: TEST OK [0.005 secs, 3508 KB] Test 11: TEST OK [0.003 secs, 3508 KB] All tests OK. Your program ('hamming') produced all correct answers! This is your submission #8 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 16 7 3 ------- test 2 ---- 2 7 7 ------- test 3 ---- 3 6 4 ------- test 4 ---- 5 5 1 ------- test 5 ---- 10 8 4 ------- test 6 ---- 20 6 2 ------- test 7 ---- 32 5 1 ------- test 8 ---- 50 8 2 ------- test 9 ---- 60 7 2 ------- test 10 ---- 16 8 3 ------- test 11 ---- 15 8 4 Keep up the good work! Thanks for your submission!