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

mac2025-02-24  22

国际象棋乐趣#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; else return np; } if (d < 0) { int r = (p - c); if (r <= 0) r += 1; int np = Math.Abs(r % (x * 2)); if (np >= x) return x * 2 - 1 - np; else return np; } return p; } } }

答案3:

namespace myjinxin { using System; public class Kata { public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k){ int y = k % (BoardSize[0] * 4); int x = k % (BoardSize[1] * 4); Console.WriteLine("y=" + y); while (y > 0) { if (BoardSize[0] * 2 <= y) y -= BoardSize[0] * 2; else if (InitPosition[0] + InitDirection[0] >=0 &amp;&amp; InitDirection[0] + InitPosition[0] < BoardSize[0]) { InitPosition[0]+=InitDirection[0]; y--; } else { InitDirection[0] *= -1; y--; } } while (x > 0) { if (BoardSize[1] * 2 <= x ) x -= BoardSize[1] * 2; else if (InitPosition[1] + InitDirection[1] >=0 &amp;&amp; InitPosition[1] + InitDirection[1] < BoardSize[1]) { InitPosition[1]+=InitDirection[1]; x--; } else { InitDirection[1] *= -1; x--; } } return InitPosition; } } }

答案4:

using System; using System.Collections.Generic; using System.Linq; namespace myjinxin { public class Kata { public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k) { // Throw out invalid input if (BoardSize == null || BoardSize.Length != 2 || BoardSize.Count(i => i >= 1 &amp;&amp; i <= 20) != 2) throw new ArgumentException("Array must be of length 2 with values between 1 and 20", "BoardSize"); if (InitPosition == null || InitPosition.Length != 2 || InitPosition.Where((i, index) => i >= 0 &amp;&amp; i < BoardSize[index]).Count() != 2) throw new ArgumentException("Array must be of length 2 with values greater than 0 and bounded by the board size", "InitPosition"); if (InitDirection == null || InitDirection.Length != 2 || InitDirection.Count(i => Math.Abs(i) == 1) != 2) throw new ArgumentException("Array must be of length 2 with values of 1 or -1", "InitDirection"); if (k < 1 || k > 1000000000) throw new ArgumentException("Value must be between 1 and 1,000,000,000", "k"); return Enumerable.Range(0, 2).Select(i => ComputeNewPosition(BoardSize[i], InitDirection[i], InitPosition[i], k)).ToArray(); } private int ComputeNewPosition(int dimension, int direction, int position, int k) { var roundTrip = dimension * 2; var range = Enumerable.Range(0, dimension); var locations = Enumerable.Concat(range, range.Reverse()); var index = (position + (direction * k)) % roundTrip; if (index < 0) index += roundTrip; return locations.ElementAt(index); } } }

答案5:

namespace myjinxin { using System; public class Kata { public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k){ int[] pos = new int[2]; int[] dir = new int[2]; Array.Copy(InitPosition, pos, 2); Array.Copy(InitDirection, dir, 2); for (int step = 1; step <= k; step++) { if ( (pos[0] + dir[0] < 0 || pos[0] + dir[0] >= BoardSize[0]) &amp;&amp; (pos[1] + dir[1] < 0 || pos[1] + dir[1] >= BoardSize[1]) ) for (int i = 0; i < 2; i++) dir[i] *= -1; else if (pos[0] + dir[0] < 0 || pos[0] + dir[0] >= BoardSize[0]) { dir[0] *= -1; pos[1] += dir[1]; } else if (pos[1] + dir[1] < 0 || pos[1] + dir[1] >= BoardSize[1]) { dir[1] *= -1; pos[0] += dir[0]; } else for (int i = 0; i < 2; i++) pos[i] += dir[i]; if (pos[0] == InitPosition[0] &amp;&amp; pos[1] == InitPosition[1] &amp;&amp; dir[0] == InitDirection[0] &amp;&amp; dir[1] == InitDirection[1]) step = k - (k % step); } return pos; } } }

答案6:

namespace myjinxin { public class Kata { public int[] ChessBishopDream(int[] boardSize, int[] initPosition, int[] initDirection, int k) { var pos0 = initPosition[0]; var pos1 = initPosition[1]; var dir0 = initDirection[0]; var dir1 = initDirection[1]; for (var i = 0; i < k; i++) { pos0 += dir0; if (pos0 < 0 || pos0 >= boardSize[0]) { dir0 = -dir0; pos0 += dir0; } pos1 += dir1; if (pos1 < 0 || pos1 >= boardSize[1]) { dir1 = -dir1; pos1 += dir1; } if (pos0 == initPosition[0] &amp;&amp; pos1 == initPosition[1] &amp;&amp; dir0 == initDirection[0] &amp;&amp; dir1 == initDirection[1] &amp;&amp; k/(i+1) > 2) { return ChessBishopDream(boardSize, initPosition, initDirection, k % (i + 1)); } } return new int[] { pos0, pos1 }; } } }

答案7:

namespace myjinxin { using System; public class Kata { public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k) { for (int kx = k % (BoardSize[0] * 2); kx >= 1; kx--) { Evaluate(ref InitPosition[0], ref InitDirection[0], BoardSize[0]); } for (int ky = k % (BoardSize[1] * 2); ky >= 1; ky--) { Evaluate(ref InitPosition[1], ref InitDirection[1], BoardSize[1]); } return InitPosition; } private static void Evaluate(ref int position, ref int direction, int size) { position += direction; direction *= ToOne(position >= 0 &amp;&amp; position < size); position += Convert.ToInt32(position < 0) - Convert.ToInt32(position >= size); } private static int ToOne(bool value) => (int)(2 * (Convert.ToDouble(value) - 0.5)); } }

答案8:

namespace myjinxin{ using System; public class Kata{ public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k){ int rows = BoardSize[0]; int cols = BoardSize[1]; k = k % ( rows * 2 *cols* 2 ); // should be lcm(2r, 2c), but this'll do... int r = InitPosition[0]; int c = InitPosition[1]; int dirR = InitDirection[0]; int dirC = InitDirection[1]; while( k > 0 ){ r += dirR; c += dirC; if( r >= rows || r < 0 ){ dirR = 0 - dirR; r += dirR; } if( c >= cols || c < 0 ){ dirC = 0 - dirC; c += dirC; } --k; } int[] output = { r, c }; return output; } } }

答案9:

namespace myjinxin { using System; public class Kata { public int[] ChessBishopDream(int[] boardSize, int[] initPosition, int[] initDirection, int k) { if (boardSize[0] < 2 &amp;&amp; boardSize[1] < 2) return new[] { 0, 0 }; var x = initPosition[0]; var y = initPosition[1]; var dx = initDirection[0]; var dy = initDirection[1]; for (int i = 0; i < k; i++) { if (x + dx < 0 &amp;&amp; y +dy < 0 || x + dx < 0 &amp;&amp; y + dy >= boardSize[1] || x + dx >= boardSize[0] &amp;&amp; y + dy < 0 || x + dx >= boardSize[0] &amp;&amp; y + dy >= boardSize[1]) { dx = -dx; dy = -dy; } else if (x + dx < 0 || x + dx >= boardSize[0]) { y += dy; dx = -dx; } else if (y + dy < 0 || y + dy >= boardSize[1]) { x += dx; dy = -dy; } else { x += dx; y += dy; } if (x == initPosition[0] &amp;&amp; y == initPosition[1] &amp;&amp; dx == initDirection[0] &amp;&amp; dy == initDirection[1]) k = k % (i + 1) + (i + 1); } return new[] { x, y }; } } }

答案10:

namespace myjinxin { using System; public class Kata { public int[] ChessBishopDream(int[] BoardSize, int[] InitPosition, int[] InitDirection, int k) { int x = test(BoardSize[0], InitPosition[0], InitDirection[0], k); int y = test(BoardSize[1], InitPosition[1], InitDirection[1], k); return new int[2] { x, y }; } public static int test (int size, int pos, int dir, int k) { int temp = k; if (dir == -1) { k = k - pos-1; } else if (dir == 1) { k = k - (size - pos); } dir *= (int)Math.Pow(-1, (k / size)+1); Console.WriteLine("Avant boucle : "+k); k %=size; Console.WriteLine("Après boucle "+k); Console.WriteLine("Direction : " + dir); if(dir == 1) { pos = k; } else if (dir ==-1) { pos = size - 1 - k; } return pos; } } }
最新回复(0)