• <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記錄開(kāi)發(fā)新知道

            中國(guó)象棋將帥問(wèn)題

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

            C語(yǔ)言版(關(guān)鍵在思路,更好的表示,特別是whoIsOk1的表示,可能編程之美上的做法更工業(yè)化,這里為了簡(jiǎn)化說(shuō)明問(wèn)題,用了很多magic number):


            /*
            * chinese_chess_admiral_marshal.c
            *
            *  Created on: 2008-9-14
            *      Author: Volnet
            *      WebSite:
            http://volnet.cnblogs.com/
            *              
            http://m.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);
            }

            值得注意的一點(diǎn)是形如#define jSET(g,j) (g=(g&0xf0)|(j))這樣的語(yǔ)句

            為了避免二義性,也為了避免被宏替換后的操作符順序依賴(lài),盡量對(duì)使用的變量單獨(dú)用()括起來(lái),因?yàn)樗皇且粋€(gè)字母在戰(zhàn)斗……-_-"

            執(zhí)行結(jié)果(片段):

            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 閱讀(883) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C/C++

            特殊功能
             
            性欧美大战久久久久久久久| 久久精品国产亚洲AV无码偷窥| 久久r热这里有精品视频| 久久精品水蜜桃av综合天堂| 久久亚洲高清观看| 久久久久国产精品麻豆AR影院| 合区精品久久久中文字幕一区| 大香伊人久久精品一区二区| 欧美丰满熟妇BBB久久久| 一本大道加勒比久久综合| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久91精品综合国产首页| 欧美精品丝袜久久久中文字幕| 婷婷伊人久久大香线蕉AV | 久久国产亚洲高清观看| 久久久亚洲精品蜜桃臀| 99999久久久久久亚洲| 合区精品久久久中文字幕一区| 夜夜亚洲天天久久| 久久狠狠高潮亚洲精品| 久久精品中文无码资源站| 久久无码AV中文出轨人妻| www.久久热.com| 久久无码人妻一区二区三区午夜| 亚洲欧美国产精品专区久久| 青青草原综合久久| 久久久青草青青亚洲国产免观| 伊人久久大香线蕉av不变影院| 精品国产乱码久久久久软件| 国产高清美女一级a毛片久久w| 久久99精品久久久久久hb无码 | 国产真实乱对白精彩久久| 国产69精品久久久久777| 97久久久精品综合88久久| 久久久久国产精品人妻| 无码人妻久久一区二区三区蜜桃| 久久男人中文字幕资源站| 久久精品女人天堂AV麻| 国产激情久久久久影院| 亚洲国产精品综合久久一线| 久久人人爽人爽人人爽av|