C#练习题答案: 国际象棋乐趣#6:国际象棋主教梦【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

这篇博客提供了10个不同解答,涉及3级难度的C#编程练习,内容围绕国际象棋中主教的移动路径问题。适合C#学习者和面试准备者提升技能。
摘要由CSDN通过智能技术生成

国际象棋乐趣#6:国际象棋主教梦【难度:3级】:

答案1:

namespace myjinxin
{
   
    using System;
    
    public class Kata
    {
   
        public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k){
   
        
        int x = InitPosition[1];
        int y = InitPosition[0];
        int dx = InitDirection[1];
        int dy = InitDirection[0];
        
        bool flag = true;  //loop until we have a pattern.
        int count = 0; //to se how many iterations it takes to find a pattern.
            
        while(flag)//look until we have a pattern.
        {
   
           count++;
           
           //do moove.
           x += dx;
           y += dy;
           
           //if out, invert direction, take back moove. 
           if(x<0)
           {
   
               x = 0;
               dx *= -1;
           }
           
           if(x>BoardSize[1]-1)
           {
   
               x = BoardSize[1]-1;
               dx *= -1;
           }
           
           if(y<0)
           {
   
               y = 0;
               dy *= -1;
           }
           
           if(y>BoardSize[0]-1)
           {
   
               y = BoardSize[0]-1;
               dy *= -1;
           }
           
           //if we end up having same x,y,dx,dy as start values, terminate, we have a pattern.
           if((x == InitPosition[1]) &amp;&amp; (dx == InitDirection[1]) &amp;&amp;
              (y == InitPosition[0]) &amp;&amp; (dy == InitDirection[0]))
               flag = false;
               
        }//end while
        
        //Now we need to take modulo of that, from the initial k value...
        //to get the x:th position. We know the steps, or count to get there. 
        int idx = k % count;
        
        //iterate to that position....
        for(int i = 0; i < idx; i++)
        {
   
           //do moove.
           x += dx;
           y += dy;
           
           //if out, invert direction, take back moove. 
           if(x<0)
           {
   
               x = 0;
               dx *= -1;
           }
           
           if(x>BoardSize[1]-1)
           {
   
               x = BoardSize[1]-1;
               dx *= -1;
           }
           
           if(y<0)
           {
   
               y = 0;
               dy *= -1;
           }
           
           if(y>BoardSize[0]-1)
           {
   
               y = BoardSize[0]-1;
               dy *= -1;
           }
        }
        
        //we have the position. Just send it back. 
        int[] pos = {
   y,x};
        return pos;
          
        }
    }
}

答案2:

namespace myjinxin
{
   
    using System;
    
    public class Kata
    {
   
        public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k){
   
          int x = getPos(BoardSize[0], InitPosition[0], InitDirection[0], k);
          int y = getPos(BoardSize[1], InitPosition[1], InitDirection[1], k);

          return new int[] {
   x,y};
        }
        
        private static int getPos(int x, int p, int d, int c)
        {
   
            if (d > 0)
            {
   
                int np = (c + p) % (x * 2);
                if (np >= x) return x * 2 - 1 - np
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值