這是編程之美之中的一道題,編程之美中的解法固然好,但自己手寫一遍還是印象深刻。話說,只有想不到,沒有做不到。
C語言版(關鍵在思路,更好的表示,特別是whoIsOk1的表示,可能編程之美上的做法更工業化,這里為了簡化說明問題,用了很多magic number):
/*
* chinese_chess_admiral_marshal.c
*
* Created on: 2008-9-14
* Author: Volnet
* WebSite: http://volnet.cnblogs.com/
* http://www.shnenglu.com/mymsdn/
*/
#include <stdio.h>
#include <stdlib.h>
/*
* admiral
* 1 2 3
* 4 5 6
* 7 8 9
*
* marshal
* 1 2 3
* 4 5 6
* 7 8 9
*
* who can't sit in the same column.
* e.g. if admiral in "1", the marshal can't in the "1"、"4"、"7".
* */
/*
* The five functions do the same thing.
* */
void whoIsOk1();
void whoIsOk2();
void whoIsOk3();
void whoIsOk4();
void whoIsOk5();
int main(void) {
whoIsOk1();
printf("-----------------------------------\n");
whoIsOk2();
printf("-----------------------------------\n");
whoIsOk3();
printf("-----------------------------------\n");
whoIsOk4();
printf("-----------------------------------\n");
whoIsOk5();
//printf("-----------------------------------\n");
return EXIT_SUCCESS;
}
void whoIsOk1() {
/*Normal one*/
unsigned int i, j, total;
total = 0;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (i % 3 != j % 3) {
printf("A=%d,B=%d\n", i, j);
total++;
}
printf("total = %d\n", total);
}
/*
* i:iiii0000
* j:0000jjjj
* */
/* g:a variable who can contains 8 bits.
* */
#define iGET(g) ((g&0xf0)>>4)
#define jGET(g) (g&0x0f)
#define iSET(g,i) (g=(g&0x0f)|((i)<<4))
#define jSET(g,j) (g=(g&0xf0)|(j))
void whoIsOk2() {
/*only one variable except “total”*/
unsigned char ij;
/* for testing
ij = 0x53;
iSET(ij,3);
jSET(ij,5);
printf("%x\n%d\n%d\n",ij,iGET(ij),jGET(ij)); */
int total;
total = 0;
for (iSET(ij,1); iGET(ij) <= 9; iSET(ij,iGET(ij)+1))
for (jSET(ij,1); jGET(ij) <= 9; jSET(ij,jGET(ij)+1))
if (iGET(ij) % 3 != jGET(ij) % 3) {
printf("A=%d,B=%d\n", iGET(ij), jGET(ij));
total++;
}
printf("total = %d\n", total);
}
void whoIsOk3() {
/* The principle of the function is :
* get the number 19 from one variable.
* get the number 19 from the same variable.
* we know the variable i/9 would mutex with i%9.
* e.g.
* int i = 20;
* i/7 == 2;
* i%7 == 6;
*
* int j = 14;
* j/7 == 2;
* j%7 == 0;
*
* i - j == 6;
* if(k/7 from 14 to 20)
* k%7 would from 0 to 6 (total = 7);
* so we can get the double circle from one variable.
* */
unsigned int i = 81, total;
total = 0;
do {
if (i / 9 % 3 != i % 9 % 3) {
printf("A=%d,B=%d\n", i / 9 + 1, i % 9 + 1);
total++;
}
} while (i--);
printf("total = %d\n", total);
}
/*whoIsOk3 is nearly equals to whoIsOk4*/
void whoIsOk4() {
unsigned int i = 81, total;
total = 0;
while (i--) {
if (i / 9 % 3 != i % 9 % 3) {
printf("A=%d,B=%d\n", i / 9 + 1, i % 9 + 1);
total++;
}
}
printf("total = %d\n", total);
}
typedef struct {
unsigned x :4;
unsigned y :4;
} complex_counter;
void whoIsOk5() {
/*use an struct scheme to mix two variables in one*/
complex_counter i;
i.x = i.y = 0;
unsigned int total;
total = 0;
for (i.x = 1; i.x <= 9; i.x++)
for (i.y = 1; i.y <= 9; i.y++)
if (i.x % 3 != i.y % 3) {
printf("A=%d,B=%d\n", i.x, i.y);
total++;
}
printf("total = %d\n", total);
}
值得注意的一點是形如#define jSET(g,j) (g=(g&0xf0)|(j))這樣的語句
為了避免二義性,也為了避免被宏替換后的操作符順序依賴,盡量對使用的變量單獨用()括起來,因為它不是一個字母在戰斗……-_-"
執行結果(片段):
A=1,B=2
A=1,B=3
A=1,B=5
A=1,B=6
A=1,B=8
A=1,B=9
A=2,B=1
A=2,B=3
A=2,B=4
A=2,B=6
A=2,B=7
A=2,B=9
A=3,B=1
A=3,B=2
A=3,B=4
A=3,B=5
A=3,B=7
A=3,B=8
A=4,B=2
A=4,B=3
A=4,B=5
A=4,B=6
A=4,B=8
A=4,B=9
A=5,B=1
A=5,B=3
A=5,B=4
A=5,B=6
A=5,B=7
A=5,B=9
A=6,B=1
A=6,B=2
A=6,B=4
A=6,B=5
A=6,B=7
A=6,B=8
A=7,B=2
A=7,B=3
A=7,B=5
A=7,B=6
A=7,B=8
A=7,B=9
A=8,B=1
A=8,B=3
A=8,B=4
A=8,B=6
A=8,B=7
A=8,B=9
A=9,B=1
A=9,B=2
A=9,B=4
A=9,B=5
A=9,B=7
A=9,B=8
total = 54