USACO 2.1 Hamming Codes(반복)

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9

int codewords, numOfBits, hammingDist;
int max;
int result[257];

int countBit1(int num){
	int numOfBit1 = 0;
	while (num){
		num &= (num - 1);
		numOfBit1++;
	}
	return numOfBit1;
}

int findCodeword(int count){
	if (count == codewords){
		int i;
		for (i = 0; i < count; i++)
			printf("%d%c", result[i], i % 10 == 9 ? '
' : ' '); printf("
"); return 1; } int num; for (num = result[count - 1] + 1; num < max; num++){ int ok = 1; int i; for (i = 0; i < count; i++){ if (countBit1(num ^ result[i]) < hammingDist){ ok = 0; break; } } if (ok){ result[count] = num; if (findCodeword(count + 1)) return 1; } } } int main(){ #if DEBUG int testCase; for (testCase = 1; testCase <= TESTCASES; testCase++){ char inputFileName[20] = "inputX.txt"; inputFileName[5] = '1' + (testCase - 1); freopen(inputFileName, "r", stdin); printf("
#%d
", testCase); #endif scanf("%d%d%d", &codewords, &numOfBits, &hammingDist); max = 1 << numOfBits; findCodeword(1); #if DEBUG } #endif return 0; }

좋은 웹페이지 즐겨찾기