有趣编程 打印象棋中两个老帅不碰面的所有位置组合

用 1 2 3 

    4 5 6

    7 8 9 

代表A(红方将) , B(黑方帅) 在棋盘9宫格中的位置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xiangqi
{
    class Program
    {
        static void Main(string[] args)
        {
            string location = "123456789123456789";
            for (int i = 0; i < 9; i++)
            {
                for (int j = 9; j < 18; j++)
                {
                    int compare = Math.Abs(Convert.ToInt16(location.Substring(i, 1)) - Convert.ToInt16(location.Substring(j, 1)));
                    if (compare !=0 && compare != 3 && compare !=6)
                    {
                            Console.Out.WriteLine("A at {0}, B at {1}", location.Substring(i, 1), location.Substring(j, 1));
                    }
                }

            }
        }
    }
}

 

posted @ 2014-01-02 10:19  银龙  阅读(265)  评论(0)    收藏  举报