raynstard #2141(2007/08/14 04:43 GMT) [ C ] Rating0/0=0.00
総当たりがうまく思いつかなかったのでビット配列に変換してしまいました。。。 NOTの計算がやっつけぽくてあまり好ましくないですがまぁ、いっか(笑 gcc -std=c99 -Wall doukaku43.c -o a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <stdio.h> #include <memory.h> int dec2bin(char *buf, size_t bufsize, int n) { memset(buf, '\0', bufsize); while( bufsize > 0 ) { buf[bufsize-1] = n & 1; bufsize --; n >>= 1; } return 0; } int solver(const char *op[], size_t nop) { int bits = nop + 1; int result ; int n,k,m; for( k=0; k<nop; k++ ) { /* NOTは右辺のみなので値無し */ if( strcmp(op[k], "not") == 0 ) bits --; } char x[bits]; bits = 1 << bits; for( n=bits; n>0; n--) { dec2bin(x, sizeof(x), n); /* ビット配列に変換 */ result = x[0] ; if( strcmp(op[0], "not") == 0 ) result = (~x[0] & 1); for( m=0,k=0; k<nop; k++,m++ ) { if( k<nop-1 ) { /* NOTの場合は先に計算する */ if( strcmp(op[k+1], "not") == 0 ) x[m+1] = (~x[m+1] & 1); } if( strcmp(op[k], "not") == 0 ) m--; /* 計算済みなので次も同じ値を使用する */ else if( strcmp(op[k], "and") == 0 ) result &= x[m+1]; else if( strcmp(op[k], "or") == 0 ) result |= x[m+1]; else if( strcmp(op[k], "xor") == 0 ) result = (result ^ x[m+1]) & 1; else { printf("invalid opration:[%s]\n",op[k]); return -1; } } if( result == 1 ) { /* 結果出力 */ dec2bin(x, sizeof(x), n); printf("("); for( m=0; m<sizeof(x)-1; m++ ) { printf("%s, ", (x[m])?"True":"False"); } printf("%s)\n", (x[m])?"True":"False"); } } return 0; } int main(int argc, char *argv[]) { solver( (const char *[]){"and","or","not","and"}, 4); return 0; }
Rating0/0=0.00-0+
[ reply ]
raynstard
#2141()
[
C
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]