青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

MyMSDN

MyMSDN記錄開發新知道

中國象棋將帥問題

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

C語言版(關鍵在思路,更好的表示,特別是whoIsOk1的表示,可能編程之美上的做法更工業化,這里為了簡化說明問題,用了很多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);
}

值得注意的一點是形如#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 閱讀(889) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

特殊功能
 
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美精品久久一区| 久久亚洲精品伦理| 国产欧美一区在线| 欧美天天影院| 国产精品入口福利| 国产精品久久9| 国产片一区二区| 国外成人在线视频网站| 国产一区二区电影在线观看 | 午夜国产不卡在线观看视频| 亚洲精品人人| 欧美亚洲免费| 欧美精品成人在线| 国产麻豆一精品一av一免费| 国产精品一区二区久久精品| 国产欧美一区二区三区视频| 亚洲欧洲日产国码二区| 亚洲人成网在线播放| 一区二区三区产品免费精品久久75| 99国产精品一区| 欧美一级片一区| 亚洲国产精品久久久久秋霞影院| 一二美女精品欧洲| 久久精品国产久精国产思思| 欧美日韩免费观看一区二区三区 | 久热爱精品视频线路一| 亚洲精品视频免费观看| 久久精品一级爱片| 国产精品va在线| 一区二区三区免费看| 欧美国产日产韩国视频| 欧美一区二区三区成人| 国产精品一区2区| 亚洲一区二区三| 在线亚洲电影| 国产精品theporn88| 亚洲无线一线二线三线区别av| 欧美成ee人免费视频| 久久香蕉精品| 亚洲毛片一区二区| 这里只有精品视频在线| 欧美特黄a级高清免费大片a级| 99视频精品全部免费在线| 亚洲人成在线观看一区二区| 欧美激情亚洲激情| 亚洲午夜激情免费视频| 欧美亚洲综合另类| 激情一区二区三区| 亚洲激情女人| 亚洲精品1234| 国产精品二区影院| 猛男gaygay欧美视频| 蜜桃精品久久久久久久免费影院| 91久久精品国产91久久性色| 日韩一级大片在线| 国产午夜精品久久久久久久| 老司机免费视频一区二区| 欧美激情影音先锋| 亚洲欧美国产高清| 欧美成人精精品一区二区频| 午夜精品久久久99热福利| 久久夜色精品亚洲噜噜国产mv| 亚洲手机视频| 欧美国产综合视频| 欧美aa在线视频| 国产日本欧美视频| 在线综合视频| 亚洲欧美成人在线| 欧美先锋影音| 一区二区三欧美| 中文一区在线| 国产精品男女猛烈高潮激情| 亚洲第一精品久久忘忧草社区| 国产精品免费区二区三区观看| 亚洲国产毛片完整版 | 欧美精品一区二区精品网 | 亚洲综合国产激情另类一区| 久久一综合视频| 欧美freesex8一10精品| 亚洲区在线播放| 欧美日韩不卡在线| 午夜视频在线观看一区二区三区 | 亚洲人成在线观看网站高清| 亚洲国产经典视频| 欧美精品福利在线| 亚洲免费在线播放| 欧美成人精品一区二区三区| …久久精品99久久香蕉国产| 美女精品在线| 亚洲一区二区三区四区视频| 久久久91精品国产一区二区精品| 国产在线精品二区| 欧美日韩高清不卡| 欧美在线视频在线播放完整版免费观看| 久久久亚洲午夜电影| 99精品视频免费| 国产一区av在线| 欧美亚洲成人精品| 欧美福利视频在线观看| 久久福利电影| 欧美一级视频| 艳女tv在线观看国产一区| 免费日韩av片| 欧美xx视频| 久久亚洲欧美| 久久久免费av| 久久久91精品国产| 久久精品国产免费观看| 午夜精品久久久久| 亚洲视频在线观看三级| 亚洲美女黄网| 亚洲视频在线观看三级| 一区二区三区日韩欧美精品| 亚洲国产一区二区三区a毛片| 精品盗摄一区二区三区| 国产亚洲精品高潮| 国产午夜亚洲精品羞羞网站| 国产精品一区在线观看你懂的| 国产精品男人爽免费视频1| 欧美日韩午夜激情| 亚洲国产成人在线播放| 噜噜噜久久亚洲精品国产品小说| 久久国产一二区| 欧美成人日本| 在线亚洲观看| 久久久xxx| 国产精品久久久久久久久 | 亚洲精品少妇| 亚洲视频精选| 久久九九有精品国产23| 男女视频一区二区| 国产女优一区| 最近中文字幕mv在线一区二区三区四区| 亚洲国产精品一区二区www| 亚洲深夜av| 亚洲福利国产精品| 久久国产直播| 国产精品久久久久久久9999| 亚洲国产高清自拍| 欧美一区二区国产| 亚洲美女一区| 欧美va亚洲va香蕉在线| 国产一区二区三区的电影| 最新中文字幕亚洲| 久久米奇亚洲| 久久精品视频免费| 国产日韩欧美在线观看| 午夜欧美精品| 亚洲综合精品| 国产一区av在线| 性欧美1819性猛交| 亚洲性感激情| 国产欧美一区二区三区沐欲 | 亚洲免费激情| 欧美乱人伦中文字幕在线| 亚洲精品国产精品国自产观看浪潮 | 国产专区一区| 欧美一区1区三区3区公司| 亚洲一二三区精品| 欧美三级不卡| 欧美在线视频播放| 欧美一区二区成人| 亚洲高清久久| 亚洲美女在线观看| 国产在线观看91精品一区| 欧美国产精品专区| 国产精品乱码| 欧美激情国产日韩精品一区18| 欧美国产欧美综合| 欧美精选午夜久久久乱码6080| 狠狠狠色丁香婷婷综合激情| 香蕉久久夜色精品| 久久精品观看| 亚洲一区二区综合| 久久青草福利网站| 一区二区免费在线播放| 久久都是精品| 一区二区三区日韩欧美精品| 欧美专区亚洲专区| 亚洲欧美日韩综合国产aⅴ| 久久精品国产亚洲精品| 亚洲日韩中文字幕在线播放| 午夜视频在线观看一区二区三区| 在线观看中文字幕不卡| 亚洲欧美国产va在线影院| 亚洲欧洲久久| 欧美午夜在线| 免费高清在线一区| 欧美体内谢she精2性欧美| 一区二区三区国产在线| 欧美性猛交xxxx乱大交退制版 | 国产精品久久久久9999吃药| 亚洲欧美精品suv| 猫咪成人在线观看| 欧美好骚综合网| 亚洲女同精品视频| 中国av一区| 国产精品入口66mio| 亚洲综合激情| 欧美图区在线视频|