• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            MyMSDN

            MyMSDN記錄開發新知道

            中國象棋將帥問題

            這是編程之美之中的一道題,編程之美中的解法固然好,但自己手寫一遍還是印象深刻。話說,只有想不到,沒有做不到。

            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

            posted on 2008-09-14 11:57 volnet 閱讀(880) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            特殊功能
             
            国内精品久久久久影院日本| 区久久AAA片69亚洲| 香蕉久久一区二区不卡无毒影院| 国产精品久久国产精品99盘| 精品综合久久久久久88小说| 国内精品久久久久影院老司 | 2021久久精品免费观看| 精品综合久久久久久97| 伊人久久综在合线亚洲2019| 国内精品久久久久影院亚洲| 久久综合久久综合久久| 伊人久久大香线蕉av不卡| 超级碰久久免费公开视频| 久久精品国产色蜜蜜麻豆| 久久婷婷国产麻豆91天堂| 东方aⅴ免费观看久久av| 亚洲成色999久久网站| 亚洲精品国产字幕久久不卡| 色婷婷噜噜久久国产精品12p| 国产精品久久一区二区三区| 精品人妻伦九区久久AAA片69| 成人精品一区二区久久久| 999久久久免费精品国产| 亚洲国产精品综合久久网络| 国产精品无码久久久久| 久久精品亚洲精品国产色婷| 久久亚洲国产精品成人AV秋霞| 国产免费久久精品丫丫| 91精品国产91久久久久久| 成人久久精品一区二区三区| 亚洲人成伊人成综合网久久久| 久久99精品久久久久久野外| 夜夜亚洲天天久久| 东京热TOKYO综合久久精品| 日产精品久久久一区二区| 伊人久久精品无码av一区| 久久精品久久久久观看99水蜜桃| 香蕉久久永久视频| 久久久精品国产| 无码人妻精品一区二区三区久久| 亚洲精品乱码久久久久久蜜桃不卡 |